diff --git a/CHANGELOG.md b/CHANGELOG.md index 04e5fdaf..52e96415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - Minor: Add an option for the reduced opacity of message history. (#6121) - Minor: Make paused chat indicator more visible, and fix its zoom behavior. (#6123) - Minor: Added interactive REPL for plugins. (#6120) -- Minor: Added WebSocket API for plugins. (#6076, #6186) +- Minor: Added WebSocket API for plugins. (#6076, #6186, #6314) - Minor: Allow for themes to set transparent values for window background on Linux. (#6137) - Minor: Popup overlay now only draws an outline when being interacted with. (#6140) - Minor: Made filters searchable in the Settings dialog search bar. (#5890) diff --git a/src/controllers/plugins/api/WebSocket.cpp b/src/controllers/plugins/api/WebSocket.cpp index e19e1abd..90464770 100644 --- a/src/controllers/plugins/api/WebSocket.cpp +++ b/src/controllers/plugins/api/WebSocket.cpp @@ -144,7 +144,10 @@ void WebSocketListenerProxy::onClose(std::unique_ptr self) auto cb = std::move(strong->onClose); strong->onText.reset(); strong->onBinary.reset(); - loggedVoidCall(cb, u"WebSocket.on_close", strong->plugin); + if (cb) + { + loggedVoidCall(cb, u"WebSocket.on_close", strong->plugin); + } } }); } @@ -154,7 +157,7 @@ void WebSocketListenerProxy::onTextMessage(QByteArray data) auto target = this->target; runInGuiThread([target, data{std::move(data)}] { auto strong = target.lock(); - if (strong) + if (strong && strong->onText) { loggedVoidCall(strong->onText, u"WebSocket.on_text", strong->plugin, data); @@ -167,7 +170,7 @@ void WebSocketListenerProxy::onBinaryMessage(QByteArray data) auto target = this->target; runInGuiThread([target, data{std::move(data)}] { auto strong = target.lock(); - if (strong) + if (strong && strong->onBinary) { loggedVoidCall(strong->onBinary, u"WebSocket.on_binary", strong->plugin, data); diff --git a/tests/src/Plugins.cpp b/tests/src/Plugins.cpp index 9f20f263..ab87d7ac 100644 --- a/tests/src/Plugins.cpp +++ b/tests/src/Plugins.cpp @@ -816,4 +816,27 @@ TEST_F(PluginTest, testWebSocketApi) ASSERT_TRUE(ok); } +TEST_F(PluginTest, testWebSocketUnsetFns) +{ + configure({PluginPermission{{{"type", "Network"}}}}); + + RequestWaiter waiter; + lua->set("done", [&] { + waiter.requestDone(); + }); + + lua->script(R"lua( + local ws = c2.WebSocket.new("wss://127.0.0.1:9050/echo") + ws.on_close = function() + done() + end + ws:send_text("message1") + ws:send_text("message2") + ws:send_binary("message3") + ws:send_binary("/CLOSE") + )lua"); + + waiter.waitForRequest(); +} + #endif