Don't require all websocket handlers, fix up Autobahn test
This commit is contained in:
parent
d8937c61d8
commit
dcfbd23146
@ -33,15 +33,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
/* idleTimeout */
|
/* idleTimeout */
|
||||||
behavior.idleTimeout = behaviorObject->Get(String::NewFromUtf8(isolate, "idleTimeout"))->Int32Value();
|
behavior.idleTimeout = behaviorObject->Get(String::NewFromUtf8(isolate, "idleTimeout"))->Int32Value();
|
||||||
|
|
||||||
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
|
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated. This is actually the enum */
|
||||||
int compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
|
behavior.compression = (uWS::CompressOptions) behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
|
||||||
if (compression == 0) {
|
|
||||||
behavior.compression = uWS::CompressOptions::DISABLED;
|
|
||||||
} else if (compression == 1) {
|
|
||||||
behavior.compression = uWS::CompressOptions::SHARED_COMPRESSOR;
|
|
||||||
} else if (compression == 2) {
|
|
||||||
behavior.compression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open */
|
/* Open */
|
||||||
openPf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
|
openPf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
|
||||||
@ -53,6 +46,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
closePf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
|
closePf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Open handler is NOT optional for the wrapper */
|
||||||
behavior.open = [openPf = std::move(openPf)](auto *ws, auto *req) {
|
behavior.open = [openPf = std::move(openPf)](auto *ws, auto *req) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
@ -69,34 +63,44 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
perSocketData->socketPf = new Persistent<Object>;
|
perSocketData->socketPf = new Persistent<Object>;
|
||||||
perSocketData->socketPf->Reset(isolate, wsObject);
|
perSocketData->socketPf->Reset(isolate, wsObject);
|
||||||
|
|
||||||
Local<Value> argv[] = {wsObject, reqObject};
|
Local<Function> openLf = Local<Function>::New(isolate, openPf);
|
||||||
Local<Function>::New(isolate, openPf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
if (!openLf->IsUndefined()) {
|
||||||
|
Local<Value> argv[] = {wsObject, reqObject};
|
||||||
|
openLf->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
/* Message handler is always optional */
|
||||||
HandleScope hs(isolate);
|
if (messagePf != Undefined(isolate)) {
|
||||||
|
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
||||||
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
||||||
|
|
||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
|
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
|
||||||
messageArrayBuffer,
|
messageArrayBuffer,
|
||||||
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
|
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
|
||||||
Local<Function>::New(isolate, messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
Local<Function>::New(isolate, messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
||||||
|
|
||||||
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
|
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
|
||||||
messageArrayBuffer->Neuter();
|
messageArrayBuffer->Neuter();
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) {
|
/* Drain handler is always optional */
|
||||||
HandleScope hs(isolate);
|
if (drainPf != Undefined(isolate)) {
|
||||||
|
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) {
|
||||||
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
|
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
|
||||||
};
|
};
|
||||||
Local<Function>::New(isolate, drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
Local<Function>::New(isolate, drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* These are not hooked in */
|
||||||
behavior.ping = [](auto *ws) {
|
behavior.ping = [](auto *ws) {
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -105,19 +109,23 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Close handler is NOT optional for the wrapper */
|
||||||
behavior.close = [closePf = std::move(closePf)](auto *ws, int code, std::string_view message) {
|
behavior.close = [closePf = std::move(closePf)](auto *ws, int code, std::string_view message) {
|
||||||
HandleScope hs(isolate);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
||||||
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
||||||
Local<Object> wsObject = Local<Object>::New(isolate, *(perSocketData->socketPf));
|
Local<Object> wsObject = Local<Object>::New(isolate, *(perSocketData->socketPf));
|
||||||
Local<Value> argv[3] = {wsObject,
|
|
||||||
Integer::New(isolate, code),
|
|
||||||
messageArrayBuffer};
|
|
||||||
|
|
||||||
/* Invalidate this wsObject */
|
/* Invalidate this wsObject */
|
||||||
wsObject->SetAlignedPointerInInternalField(0, nullptr);
|
wsObject->SetAlignedPointerInInternalField(0, nullptr);
|
||||||
Local<Function>::New(isolate, closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
|
||||||
|
/* Only call close handler if we have one set */
|
||||||
|
Local<Function> closeLf = Local<Function>::New(isolate, closePf);
|
||||||
|
if (!closeLf->IsUndefined()) {
|
||||||
|
Local<Value> argv[3] = {wsObject, Integer::New(isolate, code), messageArrayBuffer};
|
||||||
|
closeLf->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
||||||
|
}
|
||||||
|
|
||||||
delete perSocketData->socketPf;
|
delete perSocketData->socketPf;
|
||||||
|
|
||||||
|
@ -108,6 +108,11 @@ void Main(Local<Object> exports) {
|
|||||||
/* Expose some µSockets functions directly under uWS namespace */
|
/* Expose some µSockets functions directly under uWS namespace */
|
||||||
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());
|
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());
|
||||||
|
|
||||||
|
/* Compression enum */
|
||||||
|
exports->Set(String::NewFromUtf8(isolate, "DISABLED"), Integer::NewFromUnsigned(isolate, uWS::DISABLED));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate, "SHARED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::SHARED_COMPRESSOR));
|
||||||
|
exports->Set(String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR));
|
||||||
|
|
||||||
/* The template for websockets */
|
/* The template for websockets */
|
||||||
WebSocketWrapper::initWsTemplate<0>();
|
WebSocketWrapper::initWsTemplate<0>();
|
||||||
WebSocketWrapper::initWsTemplate<1>();
|
WebSocketWrapper::initWsTemplate<1>();
|
||||||
|
@ -14,16 +14,15 @@ function listenWithSettings(settings) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Create the app */
|
/* Create the app */
|
||||||
let app = settings.ssl ? uWS.App(sslOptions) : uWS.SSLApp(sslOptions);
|
let app = settings.ssl ? uWS.SSLApp(sslOptions) : uWS.App(sslOptions);
|
||||||
|
|
||||||
/* Attach our behavior from settings */
|
/* Attach our behavior from settings */
|
||||||
app.ws('/*', {
|
app.ws('/*', {
|
||||||
compression: settings.compression,
|
compression: settings.compression,
|
||||||
maxPayloadLength: 16 * 1024 * 1024,
|
maxPayloadLength: 16 * 1024 * 1024,
|
||||||
idleTimeout: 60,
|
idleTimeout: 60,
|
||||||
|
|
||||||
message: (ws, message, isBinary) => {
|
message: (ws, message, isBinary) => {
|
||||||
ws.send(message, isBinary);
|
ws.send(message, isBinary, true);
|
||||||
}
|
}
|
||||||
}).any('/exit', (res, req) => {
|
}).any('/exit', (res, req) => {
|
||||||
/* Shut down everything on this route */
|
/* Shut down everything on this route */
|
||||||
@ -56,21 +55,21 @@ function listenWithSettings(settings) {
|
|||||||
listenWithSettings({
|
listenWithSettings({
|
||||||
port: 9001,
|
port: 9001,
|
||||||
ssl: false,
|
ssl: false,
|
||||||
compression: 0
|
compression: uWS.DISABLED
|
||||||
});
|
});
|
||||||
|
|
||||||
/* SSL, shared compressor */
|
/* SSL, shared compressor */
|
||||||
listenWithSettings({
|
listenWithSettings({
|
||||||
port: 9002,
|
port: 9002,
|
||||||
ssl: true,
|
ssl: true,
|
||||||
compression: 1
|
compression: uWS.SHARED_COMPRESSOR
|
||||||
});
|
});
|
||||||
|
|
||||||
/* non-SSL, dedicated compressor */
|
/* non-SSL, dedicated compressor */
|
||||||
listenWithSettings({
|
listenWithSettings({
|
||||||
port: 9003,
|
port: 9003,
|
||||||
ssl: false,
|
ssl: false,
|
||||||
compression: 2
|
compression: uWS.DEDICATED_COMPRESSOR
|
||||||
});
|
});
|
||||||
|
|
||||||
/* This is required to check for memory leaks */
|
/* This is required to check for memory leaks */
|
||||||
@ -78,4 +77,4 @@ process.on('exit', () => {
|
|||||||
apps.forEach((a) => {
|
apps.forEach((a) => {
|
||||||
a.app.forcefully_free();
|
a.app.forcefully_free();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user