Websockets
The 'websockets' module provides Client and Server websocket functionality
Function Signatures
/* Client functions */
// Initializes a WebSocket client.
const init_client = () => Pointer
// Connects a WebSocket client to the specified URL with optional headers.
const connect_client = (client: Pointer, url: String, headers: Object = {})
=> Pointer
// Runs the WebSocket client.
const run_client = (client: Pointer) => None
// Sets up a callback for handling incoming messages on the WebSocket client.
const on_message_client = (client: Pointer, callback: Function) => None
// Sets up a callback for handling the WebSocket client's open event.
const on_open_client = (client: Pointer, callback: Function) => None
// Sets up a callback for handling the WebSocket client's close event.
const on_close_client = (client: Pointer, callback: Function) => None
// Sets up a callback for handling the WebSocket client's failure event.
const on_fail_client = (client: Pointer, callback: Function) => None
// Sends a message through the WebSocket client.
const send_client = (client: Pointer, con: Pointer, message: String) => None
// Closes the WebSocket connection for the client with optional code and reason.
const close_client = (client: Pointer, con: Pointer, code: Number, reason: String) => None
/* Client Type */
type Client = (uri: String) => {
// The URI to connect to.
uri: uri,
// The WebSocket client pointer.
client: Pointer,
// The connection pointer.
connection: Pointer,
// Flag indicating if the client is connected.
connected: Boolean,
// Flag indicating if reconnection is enabled.
reconnect: Boolean,
// Timeout (in seconds) between reconnection attempts.
reconnect_timeout: Number,
// Maximum number of reconnection attempts.
reconnect_attempts: Number,
// Counter for reconnection attempts.
reconnect_attempts_count: Number,
// Optional headers for the connection.
headers: Object,
// Connects the client to the WebSocket server with optional headers.
connect: (headers: Object = {}) => None,
// Starts running the WebSocket client.
run: () => None,
// Sends a message through the WebSocket connection.
send: (message: String) => None,
// Sets up a callback function to handle the WebSocket client's open event.
set_on_open: (callback: Function) => None,
// Sets up a callback function to handle the WebSocket client's close event.
set_on_close: (callback: Function) => None,
// Sets up a callback function to handle the WebSocket client's failure event.
set_on_fail: (callback: Function) => None,
// Sets up a callback function to handle incoming messages on the WebSocket client.
set_on_message: (callback: Function) => None,
// Closes the WebSocket connection with optional code and reason.
close: (code: Number = 1000, reason: String = "Normal") => None,
// Enables or disables reconnection.
set_reconnect: (state: Boolean) => None,
// Sets the timeout (in seconds) between reconnection attempts.
set_reconnect_timeout: (s: Number) => None,
// Sets the maximum number of reconnection attempts.
set_reconnect_attempts: (count: Number) => None,
// Sets the optional headers for the WebSocket connection.
set_headers: (headers: Object) => None,
// Thread pointer for asynchronous execution.
_thread: Pointer,
// Starts running the client asynchronously in another thread.
run_async: () => None
}
---
/* Server functions */
// Initializes a WebSocket server.
const server = () => Pointer
// Starts listening for connections on the specified port.
const server_listen = (server: Pointer, port: Number) => None
// Stops the WebSocket server.
const server_stop = (server: Pointer) => None
// Broadcasts a message to all connected clients.
const server_broadcast = (server: Pointer, message: String) => None
// Sends a message to a specific client identified by ID.
const server_send = (server: Pointer, id: String, message: String) => None
// Sets up a callback function to handle client connections.
const server_on_open = (server: Pointer, callback: Function) => None
// Sets up a callback function to handle incoming messages from clients.
const server_on_message = (server: Pointer, callback: Function) => None
// Sets up a callback function to handle server failures.
const server_on_fail = (server: Pointer, callback: Function) => None
// Sets up a callback function to handle client disconnections.
const server_on_close = (server: Pointer, callback: Function) => None
// Sets up a callback function to validate incoming connections.
const server_on_validate = (server: Pointer, callback: Function) => None
// Retrieves information about connected clients.
const server_get_clients = (server: Pointer) => [{
id: Number,
name: String
}]
// Closes a connection to a specific client identified by ID.
const server_close_connection = (server: Pointer, id: String) => None
/* Server Type */
type Server = () => {
// Represents the WebSocket server instance.
server: Pointer,
// Starts listening for connections on the specified port.
listen: (port: Number) => None,
// Sends a message to a specific client identified by ID.
send: (id: Number, message: String) => None,
// Broadcasts a message to all connected clients.
broadcast: (message: String) => None,
// Stops the WebSocket server.
stop: () => None,
// Sets up a callback function to handle incoming messages.
set_on_message: (function: Function) => None,
// Sets up a callback function to handle client connections.
set_on_open: (function: Function) => None,
// Sets up a callback function to handle client disconnections.
set_on_close: (function: Function) => None,
// Sets up a callback function to handle server failures.
set_on_fail: (function: Function) => None,
// Sets up a callback function to validate incoming connections.
set_on_validate: (function: Function) => None,
// Retrieves information about connected clients.
get_clients: () => [{
id: Number,
name: String
}],
// Closes a connection to a specific client identified by ID.
close_connection: (id: Number) => None,
// Represents the thread associated with the server.
_thread: Pointer,
// Represents the port on which the server is listening.
_port: Number,
// Callback function invoked upon receiving a message from a client.
on_message: Function,
// Callback function invoked upon a new client connection.
on_open: Function,
// Callback function invoked upon a client disconnection.
on_close: Function,
// Callback function invoked upon a server failure.
on_fail: Function,
// Callback function invoked to validate incoming connections.
on_validate: Function,
// Resets the server to its initial state.
reset: () => None,
// Resets an async running server.
reset_async: () => None,
// Starts listening for connections asynchronously in another thread.
listen_async: (port: Number) => None,
// Stops a running async server.
stop_async: () => None
}
Last updated