diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index f5a699f..3f8e2ec 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -233,6 +233,26 @@ struct WebSocketWrapper { } } + /* This one is wrapped instead of iterateTopics as JS-people will put their hands in wood chipper for sure. */ + template + static void uWS_WebSocket_getTopics(const FunctionCallbackInfo &args) { + Isolate *isolate = args.GetIsolate(); + auto *ws = getWebSocket(args); + if (ws) { + + Local topicsArray = Array::New(isolate, 0); + + ws->iterateTopics([&topicsArray, isolate](std::string_view topic) { + Local topicString = String::NewFromUtf8(isolate, topic.data(), NewStringType::kNormal, topic.length()).ToLocalChecked(); + + topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing(); + }); + + /* Todo: we need to pass a copy here */ + args.GetReturnValue().Set(topicsArray); + } + } + template static Local init(Isolate *isolate) { Local wsTemplateLocal = FunctionTemplate::New(isolate); @@ -257,6 +277,9 @@ struct WebSocketWrapper { wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed)); + /* This one does not exist in C++ */ + wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getTopics", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getTopics)); + /* Create the template */ Local wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();