diff --git a/examples/HelloWorld.js b/examples/HelloWorld.js index f4d112b..269bde6 100644 --- a/examples/HelloWorld.js +++ b/examples/HelloWorld.js @@ -17,6 +17,10 @@ const app = uWS./*SSL*/App({ }, message: (ws, message, isBinary) => { ws.send(message, isBinary); + + console.log('BufferedAmount is ' + ws.getBufferedAmount()); + + ws.close(); }, drain: (ws) => { console.log('WebSocket drained'); diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index c6cb765..2d98158 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -6,17 +6,44 @@ using namespace v8; struct WebSocketWrapper { static Persistent wsTemplate[2]; - // ws.getBufferedAmount() - // ws.close(lalala) - // ws.? + template + static inline uWS::WebSocket *getWebSocket(const FunctionCallbackInfo &args) { + return ((uWS::WebSocket *) args.Holder()->GetAlignedPointerFromInternalField(0)); + } + /* Takes code, message, returns undefined */ + template + static void uWS_WebSocket_close(const FunctionCallbackInfo &args) { + int code = 0; + std::string_view message; + + if (args.Length() >= 1) { + code = args[0]->Uint32Value(); + } + + if (args.Length() >= 2) { + NativeString nativeString(args.GetIsolate(), args[1]); + message = {nativeString.getData(), nativeString.getLength()}; + } + + getWebSocket(args)->close(code, message); + } + + /* Takes nothing, returns integer */ + template + static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo &args) { + int bufferedAmount = getWebSocket(args)->getBufferedAmount(); + args.GetReturnValue().Set(Integer::New(isolate, bufferedAmount)); + } + + /* Takes message, isBinary. Returns true on success, false otherwise */ template static void uWS_WebSocket_send(const FunctionCallbackInfo &args) { NativeString nativeString(args.GetIsolate(), args[0]); - bool isBinary = args[1]->Int32Value(); + bool isBinary = args[1]->BooleanValue(); - bool ok = ((uWS::WebSocket *) args.Holder()->GetAlignedPointerFromInternalField(0))->send( + bool ok = getWebSocket(args)->send( std::string_view(nativeString.getData(), nativeString.getLength()), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT ); @@ -32,16 +59,23 @@ struct WebSocketWrapper { wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket")); } wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); - wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send)); + /* Register our functions */ + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send"), FunctionTemplate::New(isolate, uWS_WebSocket_send)); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close"), FunctionTemplate::New(isolate, uWS_WebSocket_close)); + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount"), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount)); + + /* Create the template */ Local wsObjectLocal = wsTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); wsTemplate[SSL].Reset(isolate, wsObjectLocal); } + /* This is where we output an instance */ template static Local getWsInstance() { return Local::New(isolate, wsTemplate[std::is_same::value])->Clone(); } }; +/* Fix this, should be nicer */ Persistent WebSocketWrapper::wsTemplate[2];