Skip to content

Commit d1a2556

Browse files
committed
Merge userData with wsObject
1 parent b4df9eb commit d1a2556

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

‎examples/Upgrade.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const app = uWS./*SSL*/App({
2222
context);
2323
},
2424
open: (ws) => {
25-
console.log('A WebSocket connected with URL: ' + ws.userData.url);
25+
console.log('A WebSocket connected with URL: ' + ws.url);
2626
},
2727
message: (ws, message, isBinary) => {
2828
/* Ok is false if backpressure was built up, wait for drain */

‎src/AppWrapper.h‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,31 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
102102
Isolate *isolate = perContextData->isolate;
103103
HandleScope hs(isolate);
104104

105-
printf("Open event called!\n");
106-
107-
/* Retrieve temporary userData object */
108-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
109-
110-
// if socketPf is nullptr we have nothing to copy
111-
Local<Object> userData = Local<Object>::New(isolate, *(perSocketData->socketPf));
112-
113105
/* Create a new websocket object */
114106
Local<Object> wsObject = perContextData->wsTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
115107
wsObject->SetAlignedPointerInInternalField(0, ws);
116108

117-
/* Copy entires from userData */
118-
wsObject->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "userData"), userData);
109+
/* Retrieve temporary userData object */
110+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
111+
112+
/* Copy entires from userData, only if we have it set */
113+
if (perSocketData->socketPf) {
114+
/* socketPf points to a stack allocated UniquePersistent, or nullptr, at this point */
115+
Local<Object> userData = Local<Object>::New(isolate, *(perSocketData->socketPf));
116+
117+
/* Merge userData and wsObject; this code is exceedingly horrible */
118+
Local<Array> keys;
119+
if (userData->GetOwnPropertyNames(isolate->GetCurrentContext()).ToLocal(&keys)) {
120+
for (int i = 0; i < keys->Length(); i++) {
121+
wsObject->Set(isolate->GetCurrentContext(),
122+
keys->Get(isolate->GetCurrentContext(), i).ToLocalChecked(),
123+
userData->Get(isolate->GetCurrentContext(), keys->Get(isolate->GetCurrentContext(), i).ToLocalChecked()).ToLocalChecked()
124+
);
125+
}
126+
}
127+
}
119128

120-
/* Attach a new V8 object with pointer to us, to us */
129+
/* Attach a new V8 object with pointer to us, to it */
121130
perSocketData->socketPf = new UniquePersistent<Object>;
122131
perSocketData->socketPf->Reset(isolate, wsObject);
123132

‎src/HttpResponseWrapper.h‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,17 @@ struct HttpResponseWrapper {
248248
}
249249
}
250250

251+
/* Takes UserData, secKey, secProtocol, secExtensions, context. Returns nothing */
251252
template <bool SSL>
252253
static void res_upgrade(const FunctionCallbackInfo<Value> &args) {
253254
Isolate *isolate = args.GetIsolate();
254255
auto *res = getHttpResponse<SSL>(args);
255256
if (res) {
256-
257-
printf("Calling upgrade!\n");
258-
257+
/* We require exactly 5 arguments */
259258
if (args.Length() != 5) {
260259
return;
261260
}
262261

263-
/* We are being passed userData (wsObject) */
264-
//Local<Object> wsObject = args[0];
265-
//Local<String> secWebSocketKey = args[1];
266-
//Local<String> secWebSocketProtocol = args[2];
267-
//Local<String> secWebSocketExtensions = args[3];
268-
//Local<External> context = args[4];
269-
270-
271262
NativeString secWebSocketKey(args.GetIsolate(), args[1]);
272263
if (secWebSocketKey.isInvalid(args)) {
273264
return;
@@ -287,10 +278,11 @@ struct HttpResponseWrapper {
287278

288279
invalidateResObject(args);
289280

281+
/* This releases on return */
290282
UniquePersistent<Object> userData;
291283
userData.Reset(isolate, Local<Object>::Cast(args[0]));
292284

293-
printf("Upgrading now!\n");
285+
/* Immediately calls open handler */
294286
res->template upgrade<PerSocketData>({
295287
.socketPf = &userData
296288
}, secWebSocketKey.getString(), secWebSocketProtocol.getString(),

0 commit comments

Comments
 (0)