Merge userData with wsObject
This commit is contained in:
parent
b4df9ebf5f
commit
d1a2556fba
2
examples/Upgrade.js
vendored
2
examples/Upgrade.js
vendored
@ -22,7 +22,7 @@ const app = uWS./*SSL*/App({
|
||||
context);
|
||||
},
|
||||
open: (ws) => {
|
||||
console.log('A WebSocket connected with URL: ' + ws.userData.url);
|
||||
console.log('A WebSocket connected with URL: ' + ws.url);
|
||||
},
|
||||
message: (ws, message, isBinary) => {
|
||||
/* Ok is false if backpressure was built up, wait for drain */
|
||||
|
@ -102,22 +102,31 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
||||
Isolate *isolate = perContextData->isolate;
|
||||
HandleScope hs(isolate);
|
||||
|
||||
printf("Open event called!\n");
|
||||
|
||||
/* Retrieve temporary userData object */
|
||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||
|
||||
// if socketPf is nullptr we have nothing to copy
|
||||
Local<Object> userData = Local<Object>::New(isolate, *(perSocketData->socketPf));
|
||||
|
||||
/* Create a new websocket object */
|
||||
Local<Object> wsObject = perContextData->wsTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
|
||||
wsObject->SetAlignedPointerInInternalField(0, ws);
|
||||
|
||||
/* Copy entires from userData */
|
||||
wsObject->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "userData"), userData);
|
||||
/* Retrieve temporary userData object */
|
||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||
|
||||
/* Attach a new V8 object with pointer to us, to us */
|
||||
/* Copy entires from userData, only if we have it set */
|
||||
if (perSocketData->socketPf) {
|
||||
/* socketPf points to a stack allocated UniquePersistent, or nullptr, at this point */
|
||||
Local<Object> userData = Local<Object>::New(isolate, *(perSocketData->socketPf));
|
||||
|
||||
/* Merge userData and wsObject; this code is exceedingly horrible */
|
||||
Local<Array> keys;
|
||||
if (userData->GetOwnPropertyNames(isolate->GetCurrentContext()).ToLocal(&keys)) {
|
||||
for (int i = 0; i < keys->Length(); i++) {
|
||||
wsObject->Set(isolate->GetCurrentContext(),
|
||||
keys->Get(isolate->GetCurrentContext(), i).ToLocalChecked(),
|
||||
userData->Get(isolate->GetCurrentContext(), keys->Get(isolate->GetCurrentContext(), i).ToLocalChecked()).ToLocalChecked()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Attach a new V8 object with pointer to us, to it */
|
||||
perSocketData->socketPf = new UniquePersistent<Object>;
|
||||
perSocketData->socketPf->Reset(isolate, wsObject);
|
||||
|
||||
|
@ -248,26 +248,17 @@ struct HttpResponseWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
/* Takes UserData, secKey, secProtocol, secExtensions, context. Returns nothing */
|
||||
template <bool SSL>
|
||||
static void res_upgrade(const FunctionCallbackInfo<Value> &args) {
|
||||
Isolate *isolate = args.GetIsolate();
|
||||
auto *res = getHttpResponse<SSL>(args);
|
||||
if (res) {
|
||||
|
||||
printf("Calling upgrade!\n");
|
||||
|
||||
/* We require exactly 5 arguments */
|
||||
if (args.Length() != 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* We are being passed userData (wsObject) */
|
||||
//Local<Object> wsObject = args[0];
|
||||
//Local<String> secWebSocketKey = args[1];
|
||||
//Local<String> secWebSocketProtocol = args[2];
|
||||
//Local<String> secWebSocketExtensions = args[3];
|
||||
//Local<External> context = args[4];
|
||||
|
||||
|
||||
NativeString secWebSocketKey(args.GetIsolate(), args[1]);
|
||||
if (secWebSocketKey.isInvalid(args)) {
|
||||
return;
|
||||
@ -287,10 +278,11 @@ struct HttpResponseWrapper {
|
||||
|
||||
invalidateResObject(args);
|
||||
|
||||
/* This releases on return */
|
||||
UniquePersistent<Object> userData;
|
||||
userData.Reset(isolate, Local<Object>::Cast(args[0]));
|
||||
|
||||
printf("Upgrading now!\n");
|
||||
/* Immediately calls open handler */
|
||||
res->template upgrade<PerSocketData>({
|
||||
.socketPf = &userData
|
||||
}, secWebSocketKey.getString(), secWebSocketProtocol.getString(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user