Skip to content

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 ​

new Websocket(String address)#
Sets up the underlying socket and installs the lifecycle callback listener without connecting. Call connect() to start the connection.

Parameters:

  • address: websocket server address
new Websocket(URL 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 ​

Called after connection with the underlying socket and the server's handshake headers.

onDisconnect ​

Called when the connection disconnects, with the underlying socket and disconnect details.

onError ​

Called when the websocket client reports an error, with the socket and exception.

onFrame ​

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 ​

close(): WebsocketSince1.1.9#
Starts the closing handshake using the client library's default close code.
Returns: self for chaining
close(int closeCode): WebsocketSince1.1.9#
Starts the closing handshake with the given close code; the code is passed to the client library without validation by this class.

Parameters:

  • closeCode: the code to put on the close frame
Returns: self for chaining

connect ​

connect(): WebsocketSince1.1.9#
Connects to the server. The client library performs the connection attempt.
Returns: self for chaining

getWs ​

getWs(): WebSocketSince1.1.9#
Returns the underlying websocket client object.
Returns: the underlying websocket

sendText ​

sendText(String text): WebsocketSince1.1.9#
Sends a text message through the underlying websocket client.

Parameters:

  • text: the message to send
Returns: self for chaining