API: websockets
Source:
net/websockets.ts
Provides a high-level API for managing WebSocket clients and handling lifecycle events within the application context. It wraps the internal Java WebsocketsFacade.
Usage
import { websockets } from "sdk/net"
const uri = "ws://echo.websocket.org:80/";
const handler = "my-project/ws-handler"
function initialize() {
console.log("Connect to: " + uri);
let websocket = websockets.createWebsocket(uri, handler);
websocket.send("hello");
}
initialize();
websockets.getClientByHandler(handler).close();
// my-endpoint.websocket
{
"handler": "my-project/ws-handler",
"endpoint":"my-endpoint",
"description":"My Websocket"
}
// ----------------
// from web browser
// ----------------
let ws = new WebSocket("ws://localhost:8080/websockets/service/my-endpoint");
ws.send('hello');Classes
Websockets
Websockets
Static utility class for accessing and managing WebSocket functionality.
Methods
createWebsocket
createWebsocket (uri:string, handler:string):WebsocketClientCreates a new WebSocket client connection to a specified URI, managed by a handler script.
@param uri The target WebSocket URI (e.g., 'ws://example.com/socket').
@param handler The identifier or path of the script handling the WebSocket events.
@returns A wrapper object for the new WebSocket session.
getClients
getClients ():string[]Retrieves a list of all active WebSocket clients.
@returns An array of objects detailing the URI and handler of each client.
getClient
getClient (id:string):WebsocketClient|undefinedRetrieves a specific WebSocket client wrapper by its session ID.
@param id The session ID of the client.
@returns The client wrapper or undefined if not found.
getClientByHandler
getClientByHandler (handler:string):WebsocketClient|undefinedRetrieves a specific WebSocket client wrapper by its handler identifier.
@param handler The handler identifier associated with the client.
@returns The client wrapper or undefined if not found.
getMessage
getMessage ():anyRetrieves the message payload from the current context, typically used inside an 'onmessage' handler.
@returns The message content.
getError
getError ():anyRetrieves error details from the current context, typically used inside an 'onerror' handler.
@returns The error object or string.
getMethod
getMethod ():stringRetrieves the event method name that triggered the current script execution (e.g., "onopen", "onmessage").
@returns The name of the event method.
isOnOpen
isOnOpen ():booleanChecks if the current event context is 'onopen'.
@returns True if the method is 'onopen'.
isOnMessage
isOnMessage ():booleanChecks if the current event context is 'onmessage'.
@returns True if the method is 'onmessage'.
isOnError
isOnError ():booleanChecks if the current event context is 'onerror'.
@returns True if the method is 'onerror'.
isOnClose
isOnClose ():booleanChecks if the current event context is 'onclose'.
@returns True if the method is 'onclose'.
WebsocketClient
@class WebsocketClient
@description Wrapper for a native WebSocket session, providing methods to send and close the connection.
Methods
send
send (text:string):voidSends a text message over the WebSocket connection.
@param text The message to send.
close
close ():voidCloses the WebSocket connection.