Websocket
com.jsmacrosce.jsmacros.core.library.impl.classes.Websocket
Wraps a websocket connection and exposes its lifecycle callbacks and operations.
Create it with Request.createWS; construction does not connect, so call connect() to start the connection. Assign callbacks with JavaWrapper.methodToJava; each receives the underlying socket and event data.
js
const socket = Request.createWS("wss://example.com/socket");
socket.onConnect = JavaWrapper.methodToJava(function (ws, headers) {
print("connected");
ws.sendText("hello from the script");
});
socket.onTextMessage = JavaWrapper.methodToJava(function (ws, text) {
print(`from the socket: ${text}`);
});
socket.connect();Nested types
Constructors
new Websocket
Sets up the underlying socket and installs the lifecycle callback listener without connecting. Call connect() to start the connection.
Parameters:
address: websocket server address
Sets up the underlying socket from a URL without connecting, converting it to a URI string and delegating to Websocket(String).
Parameters:
address: URL of the websocket server
Fields
onConnect
onDisconnect
onDisconnect: MethodWrapper<WebSocket, Websocket.Disconnected, Object, ?>#Called when the connection disconnects, with the underlying socket and disconnect details.
onError
onError: MethodWrapper<WebSocket, WebSocketException, Object, ?>#Called when the websocket client reports an error, with the socket and exception.
onFrame
onFrame: MethodWrapper<WebSocket, WebSocketFrame, Object, ?>#Receives the underlying socket and each received frame. The library emits this notification before any matching message notification; script execution order depends on the callback wrappers. A frame is not necessarily a complete message.
onTextMessage
onTextMessage: MethodWrapper<WebSocket, String, Object, ?>#Called with the underlying socket and the decoded text for each text message.
Methods
close
Starts the closing handshake using the client library's default close code.
Returns: self for chaining