feat(WebSocket): add open event (#6315)
This commit is contained in:
@@ -19,6 +19,7 @@ public:
|
||||
void onClose(std::unique_ptr<WebSocketListener> self) override;
|
||||
void onBinaryMessage(QByteArray data) override;
|
||||
void onTextMessage(QByteArray data) override;
|
||||
void onOpen() override;
|
||||
|
||||
private:
|
||||
std::weak_ptr<WebSocket> target;
|
||||
@@ -58,10 +59,15 @@ void WebSocket::createUserType(sol::table &c2, Plugin *plugin)
|
||||
v.as<std::string>());
|
||||
}
|
||||
}
|
||||
sol::optional<sol::main_function> onOpen = luaOpts["on_open"];
|
||||
sol::optional<sol::main_function> onText = luaOpts["on_text"];
|
||||
sol::optional<sol::main_function> onBinary =
|
||||
luaOpts["on_binary"];
|
||||
sol::optional<sol::main_function> onClose = luaOpts["on_close"];
|
||||
if (onOpen)
|
||||
{
|
||||
self->onOpen = std::move(*onOpen);
|
||||
}
|
||||
if (onText)
|
||||
{
|
||||
self->onText = std::move(*onText);
|
||||
@@ -108,6 +114,14 @@ void WebSocket::createUserType(sol::table &c2, Plugin *plugin)
|
||||
[](WebSocket &ws, sol::main_function fn) {
|
||||
ws.onBinary = std::move(fn);
|
||||
}),
|
||||
"on_open",
|
||||
sol::property(
|
||||
[](WebSocket &ws) {
|
||||
return ws.onOpen;
|
||||
},
|
||||
[](WebSocket &ws, sol::main_function fn) {
|
||||
ws.onOpen = std::move(fn);
|
||||
}),
|
||||
"close", &WebSocket::close, //
|
||||
"send_text", &WebSocket::sendText, //
|
||||
"send_binary", &WebSocket::sendBinary //
|
||||
@@ -178,6 +192,19 @@ void WebSocketListenerProxy::onBinaryMessage(QByteArray data)
|
||||
});
|
||||
}
|
||||
|
||||
void WebSocketListenerProxy::onOpen()
|
||||
{
|
||||
auto target = this->target;
|
||||
runInGuiThread([target] {
|
||||
auto strong = target.lock();
|
||||
if (strong && strong->onOpen)
|
||||
{
|
||||
loggedVoidCall(strong->onOpen, u"WebSocket.on_open",
|
||||
strong->plugin);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace chatterino::lua::api
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
* connection is made immediately.
|
||||
*
|
||||
* @lua@param url string The URL to connect to. Must start with `wss://` or `ws://`.
|
||||
* @lua@param options? { headers?: table<string, string>, on_close?: fun(), on_text?: fun(data: string), on_binary?: fun(data: string) } Additional options for the connection.
|
||||
* @lua@param options? { headers?: table<string, string>, on_close?: fun(), on_text?: fun(data: string), on_binary?: fun(data: string), on_open?: fun() } Additional options for the connection.
|
||||
* @lua@return c2.WebSocket
|
||||
* @lua@nodiscard
|
||||
* @exposed c2.WebSocket.new
|
||||
@@ -66,6 +66,10 @@ private:
|
||||
* @lua@field on_binary fun(data: string)|nil Handler called when the socket receives a binary message.
|
||||
*/
|
||||
sol::main_function onBinary;
|
||||
/**
|
||||
* @lua@field on_open fun()|nil Handler called when the websocket handshake has been completed successfully.
|
||||
*/
|
||||
sol::main_function onOpen;
|
||||
WebSocketHandle handle;
|
||||
// Note: this class lives inside the plugin -> this pointer will be valid.
|
||||
Plugin *plugin = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user