feat(plugins): add c2.WebSocket (#6076)

This commit is contained in:
nerix
2025-04-07 19:38:10 +02:00
committed by GitHub
parent d3bab9132f
commit ab66be21b3
31 changed files with 1859 additions and 29 deletions
+23 -1
View File
@@ -1,6 +1,6 @@
/** @noSelfInFile */
declare module c2 {
declare namespace c2 {
enum LogLevel {
Debug,
Info,
@@ -129,4 +129,26 @@ declare module c2 {
function register_callback<T>(type: T, func: CbFunc<T>): void;
function later(callback: () => void, msec: number): void;
interface WebSocket {
close(): void;
send_text(data: string): void;
send_binary(data: string): void;
on_close: null | (() => void);
on_text: null | ((data: string) => void);
on_binary: null | ((data: string) => void);
}
interface WebSocketConstructor {
new: (
this: void,
url: string,
options?: {
headers?: Record<string, string>;
on_close?: () => void;
on_text?: (data: string) => void;
on_binary?: (data: string) => void;
}
) => WebSocket;
}
var WebSocket: WebSocketConstructor;
}
+33
View File
@@ -257,6 +257,39 @@ function c2.HTTPRequest.create(method, url) end
-- End src/controllers/plugins/api/HTTPRequest.hpp
-- Begin src/controllers/plugins/api/WebSocket.hpp
---@class c2.WebSocket
---@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.
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.
---@return c2.WebSocket
---@nodiscard
function c2.WebSocket.new(url, options) end
--- Closes the socket.
---
function c2.WebSocket:close() end
--- Sends a text message on the socket.
---
---@param data string The text to send.
function c2.WebSocket:send_text(data) end
--- Sends a binary message on the socket.
---
---@param data string The binary data to send.
function c2.WebSocket:send_binary(data) end
-- End src/controllers/plugins/api/WebSocket.hpp
-- Begin src/common/network/NetworkCommon.hpp
---@enum c2.HTTPMethod