net WebSocket communication

net WebSocket communication

net WebSocket communication

Function

net.listen({type: 'ws'}) starts the WebSocket server, net.connect({type: 'ws'}) creates the WebSocket client.

Server syntax

Client syntax

Server configuration field

net.listen({type: 'ws', ...}) uses the following fields:

FieldTypeRequiredDefault valueDescription
typeStringYesNoneFixed to ws.
bindStringYesNoneWebSocket server listening address in host:port format, such as 127.0.0.1:9002. When the port is 0, the operating system allocates an available port.
routeStringNo/wsWebSocket handshake path. The client URL path must match this value.
binaryBoolNofalseWhen true, the message parameter of on_message on the server returns Bytes; the default behavior is to maintain String compatibility.
on_connectFnNoNoneCalled when the client connection is successful, the callback parameter is socket.
on_messageFnNoNoneCalled when a client message is received, the callback parameter is socket, message.
on_closeFnNoNoneCalled when the client connection is closed, the callback parameter is socket.
on_errorFnNoNoneCalled when the server or connection handles errors, the callback parameter is message.

The client configuration field

net.connect({type: 'ws', ...}) uses the following fields:

FieldTypeRequiredDefaultDescription
typeStringYesNoneFixed to ws.
urlStringYesNoneWebSocket complete connection address, such as ws://127.0.0.1:9002/ws.

Return Value

TypeDescription
WsServerWebSocket server handle returned by net.listen({type:'ws'}).
WsSocketWebSocket connection handle. Both the socket in the server callback and the client net.connect({type:'ws'}) return value use this type.

WsServer fields and methods

NameTypeDescription
addrStringThe actual listening address, the format is usually host:port; when the bind port is 0, the port allocated by the system can be read from here.
typeStringFixed to ws.
close()Fn -> BoolClose WebSocket server monitoring, return true successfully.

WsSocket fields and methods

NameTypeDescription
addrStringWebSocket peer address or connection target address, usually in the format host:port.
typeStringFixed to ws.
send(data)Fn -> BoolSend WebSocket message; String uses text frame, Bytes or byte array uses binary frame, returns true successfully.
write(data)Fn -> BoolSynonymous method of send(data).
close()Fn -> BoolClose the WebSocket connection and return true on success.
on_message(fn(message) {}, binary)Fn -> WsSocketClient connection handle registers message callback; when the second parameter is true, message is Bytes.
on_close(fn() {})Fn -> WsSocketClient connection handle registration close callback.
on_error(fn(message) {})Fn -> WsSocketClient connection handle registration error callback.

Server callback parameters

CallbackParametersDescription
on_connectsocket: WsSocketTriggered when the new client connection is successful.
on_messagesocket: WsSocket, message: String/BytesTriggered when a client message is received; when binary: true, message is Bytes, otherwise it is a compatible string.
on_closesocket: WsSocketTriggered when the client connection is closed.
on_errormessage: StringTriggered when the server or connection handles errors.

Examples

Notes

  • Server route defaults to /ws.
- WebSocket read() does not support synchronous reads; messages are distributed via background event callbacks.
  • send/write sends a binary frame when Bytes or a byte array is passed in; sends a text frame when a String is passed in.
  • The default message of on_message is a string; when the server listening configuration is binary: true or the client on_message(fn, true), it is Bytes.
  • WebSocket server monitoring, client connection, and message reading and writing use the shared Tokio runtime.
- A single text or binary message is limited by BT_NET_MESSAGE_LIMIT, default 1048576 bytes.
  • send/write will enter a single-connection bounded write queue. The queue length is controlled by BT_NET_WRITE_QUEUE and defaults to 1024.
- When the network event queue is full, the WebSocket message event triggers a connection close and increments BT.stats().net.event_queue_rejected.
  • When BT_NET_IDLE_TTL_MS is greater than 0, if the connection does not receive a message for more than this time, an error callback will be triggered and closed.