diff --git a/src/AppWrapper.h b/src/AppWrapper.h index f108f5e..2a8a1db 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -24,6 +24,8 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { UniquePersistent messagePf; UniquePersistent drainPf; UniquePersistent closePf; + UniquePersistent pingPf; + UniquePersistent pongPf; struct PerSocketData { UniquePersistent *socketPf; @@ -65,6 +67,11 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { drainPf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "drain", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); /* Close */ closePf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); + /* Ping */ + pingPf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); + /* Pong */ + pongPf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "pong", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); + } /* Open handler is NOT optional for the wrapper */ @@ -123,14 +130,27 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { }; } - /* These are not hooked in */ - behavior.ping = [](auto *ws) { + /* Ping handler is always optional */ + if (pingPf != Undefined(isolate)) { + behavior.ping = [pingPf = std::move(pingPf), isolate](auto *ws) { + HandleScope hs(isolate); - }; + PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); + Local argv[1] = {Local::New(isolate, *(perSocketData->socketPf))}; + CallJS(isolate, Local::New(isolate, pingPf), 1, argv); + }; + } - behavior.pong = [](auto *ws) { + /* Pong handler is always optional */ + if (pongPf != Undefined(isolate)) { + behavior.pong = [pongPf = std::move(pongPf), isolate](auto *ws) { + HandleScope hs(isolate); - }; + PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); + Local argv[1] = {Local::New(isolate, *(perSocketData->socketPf))}; + CallJS(isolate, Local::New(isolate, pongPf), 1, argv); + }; + } /* Close handler is NOT optional for the wrapper */ behavior.close = [closePf = std::move(closePf), isolate](auto *ws, int code, std::string_view message) { diff --git a/uWebSockets b/uWebSockets index 588d234..591518b 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 588d234d45064fc70c0b6871383cbed2b6ff9d20 +Subproject commit 591518b2d0749165e6c5a3aca55ee1d1c0454bdc