WebSocket

WebSocket.Send

Sends a Lua string with a WebSocket object returned by WebSocket.connect.

Syntax

WebSocket.Send(socket: WebSocket, message: string, isBinary?: boolean)

Arguments

NameTypeDescription
socketWebSocketWebSocket table returned by WebSocket.connect. Send is resolved from the WebSocket object methods, so the documented call form is socket:Send(..).
messagestringLua string payload passed to curl_ws_send with its explicit byte length.
isBinary?booleanWhen this argument is present and true, Send uses CURLWS_BINARY. Otherwise it uses CURLWS_TEXT.

Description

Sends a Lua string with a WebSocket object returned by WebSocket.connect. The method expects the WebSocket table as the first argument and the message string as the second argument. It reads the object's connection state, raises "WebSocket state is missing" if that object is absent, returns without sending when the state is destroyed or closed, and calls curl_ws_send with CURLWS_TEXT by default or CURLWS_BINARY when the optional third argument is a boolean true. It returns no values and does not report the number of bytes sent.

Call it with 3 parameter(s): socket, message, isBinary. The argument table explains which values are required and which ones only refine the behavior.

It does not return data. Treat the call as an action and handle errors around the call site when needed.

Example

Send text or binary data through a WebSocket object returned by WebSocket.connect.

local socket = WebSocket.connect("wss://example.com/socket")

socket:Send("hello")
socket:Send(string.char(0, 1, 2, 3), true)