diff --git a/src/AppWrapper.h b/src/AppWrapper.h index ba821877..1a05ce02 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -47,6 +47,7 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { UniquePersistent messagePf; UniquePersistent drainPf; UniquePersistent closePf; + UniquePersistent droppedPf; UniquePersistent pingPf; UniquePersistent pongPf; UniquePersistent subscriptionPf; @@ -107,6 +108,8 @@ 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())); + /* Dropped */ + droppedPf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "dropped", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); /* Ping */ pingPf.Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked())); /* Pong */ @@ -198,6 +201,25 @@ void uWS_App_ws(const FunctionCallbackInfo &args) { }; } + /* Dropped handler is always optional (similar to message) */ + if (droppedPf != Undefined(isolate)) { + behavior.dropped = [droppedPf = std::move(droppedPf), isolate](auto *ws, std::string_view message, uWS::OpCode opCode) { + HandleScope hs(isolate); + + Local messageArrayBuffer = ArrayBuffer_New(isolate, (void *) message.data(), message.length()); + + PerSocketData *perSocketData = (PerSocketData *) ws->getUserData(); + Local argv[3] = {Local::New(isolate, perSocketData->socketPf), + messageArrayBuffer, + Boolean::New(isolate, opCode == uWS::OpCode::BINARY)}; + + CallJS(isolate, Local::New(isolate, droppedPf), 3, argv); + + /* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */ + messageArrayBuffer->Detach(); + }; + } + /* Drain handler is always optional */ if (drainPf != Undefined(isolate)) { behavior.drain = [drainPf = std::move(drainPf), isolate](auto *ws) {