Begin WebSocketWrapper nice struct

This commit is contained in:
Alex Hultman 2019-01-14 11:54:37 +01:00
parent a6b897cdbc
commit 31e7463c9b
3 changed files with 37 additions and 37 deletions

View File

@ -66,7 +66,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
HandleScope hs(isolate); HandleScope hs(isolate);
/* Create a new websocket object */ /* Create a new websocket object */
Local<Object> wsObject = getWsInstance<APP>(); Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
wsObject->SetAlignedPointerInInternalField(0, ws); wsObject->SetAlignedPointerInInternalField(0, ws);
/* Attach a new V8 object with pointer to us, to us */ /* Attach a new V8 object with pointer to us, to us */

View File

@ -1,20 +1,17 @@
// ws.getBufferedAmount()
// ws.close(lalala)
// ws.?
/* Helping QtCreator */
#include "App.h" #include "App.h"
#include <v8.h> #include <v8.h>
#include "Utilities.h" #include "Utilities.h"
using namespace v8; using namespace v8;
// also wrap all of this in some common struct struct WebSocketWrapper {
Persistent<Object> wsTemplate[2]; static Persistent<Object> wsTemplate[2];
/* WebSocket send */ // ws.getBufferedAmount()
// ws.close(lalala)
// ws.?
template <bool SSL> template <bool SSL>
void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) { static void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
NativeString nativeString(args.GetIsolate(), args[0]); NativeString nativeString(args.GetIsolate(), args[0]);
bool isBinary = args[1]->Int32Value(); bool isBinary = args[1]->Int32Value();
@ -27,7 +24,7 @@ void uWS_WebSocket_send(const FunctionCallbackInfo<Value> &args) {
} }
template <bool SSL> template <bool SSL>
void initWsTemplate() { static void initWsTemplate() {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate); Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
if (SSL) { if (SSL) {
wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket")); wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket"));
@ -42,6 +39,9 @@ void initWsTemplate() {
} }
template <class APP> template <class APP>
Local<Object> getWsInstance() { static Local<Object> getWsInstance() {
return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone(); return Local<Object>::New(isolate, wsTemplate[std::is_same<APP, uWS::SSLApp>::value])->Clone();
} }
};
Persistent<Object> WebSocketWrapper::wsTemplate[2];

View File

@ -76,8 +76,8 @@ void Main(Local<Object> exports) {
// these inits should probably happen elsewhere (in their respective struct's constructor)? // these inits should probably happen elsewhere (in their respective struct's constructor)?
/* The template for websockets */ /* The template for websockets */
initWsTemplate<0>(); WebSocketWrapper::initWsTemplate<0>();
initWsTemplate<1>(); WebSocketWrapper::initWsTemplate<1>();
/* Initialize SSL and non-SSL templates */ /* Initialize SSL and non-SSL templates */
initResTemplate<0>(); initResTemplate<0>();