feat(WebSocket): add open event (#6315)

This commit is contained in:
nerix
2025-07-05 14:36:22 +02:00
committed by GitHub
parent 7a0de27253
commit 91c2c1021c
9 changed files with 79 additions and 6 deletions
+2
View File
@@ -133,6 +133,7 @@ declare namespace c2 {
close(): void;
send_text(data: string): void;
send_binary(data: string): void;
on_open: null | (() => void);
on_close: null | (() => void);
on_text: null | ((data: string) => void);
on_binary: null | ((data: string) => void);
@@ -143,6 +144,7 @@ declare namespace c2 {
url: string,
options?: {
headers?: Record<string, string>;
on_open?: () => void;
on_close?: () => void;
on_text?: (data: string) => void;
on_binary?: (data: string) => void;
+2 -1
View File
@@ -263,13 +263,14 @@ function c2.HTTPRequest.create(method, url) end
---@field on_close fun()|nil Handler called when the socket is closed.
---@field on_text fun(data: string)|nil Handler called when the socket receives a text message.
---@field on_binary fun(data: string)|nil Handler called when the socket receives a binary message.
---@field on_open fun()|nil Handler called when the websocket handshake has been completed successfully.
c2.WebSocket = {}
--- Creates and connects to a WebSocket server. Upon calling this, a
--- connection is made immediately.
---
---@param url string The URL to connect to. Must start with `wss://` or `ws://`.
---@param options? { headers?: table<string, string>, on_close?: fun(), on_text?: fun(data: string), on_binary?: fun(data: string) } Additional options for the connection.
---@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.
---@return c2.WebSocket
---@nodiscard
function c2.WebSocket.new(url, options) end