Modules

Docs / API / Modules / ws

WebSocket

Network

@s2script/sdk/ws

client WebSocket.

Import

import { WebSocket } from "@s2script/sdk/ws";

Author-time types ship in the npm package; the engine injects the runtime at plugin load. Add @s2script/sdk to your plugin's dependencies.

API reference

Generated from the shipped type definitions.

interface
WebSocket

An open WebSocket connection — a per-plugin handle over a copied message stream (no live socket crosses to JS).

onMessage(handler: (data: string) => void): void
Register a handler invoked for each inbound text frame.
onClose(handler: (code: number, reason: string) => void): void
Register a handler for connection close; code/reason come from the close frame.
onError(handler: (err: string) => void): void
Register a handler for a transport error; err is the error text.
send(data: string): void
Send a text frame.
close(): void
Close the connection.
const
WebSocket

Entry point for opening WebSocket connections.

connect(url: string): Promise<WebSocket>

Connect to a WebSocket server (wss:// for TLS) off the game thread.

ReturnsResolves on the open handshake with the live WebSocket handle.

ThrowsRejects on connect failure (bad URL, refused, TLS/handshake error).

import { WebSocket } from "@s2script/sdk/ws";
const ws = await WebSocket.connect("wss://ws.postman-echo.com/raw");
ws.onMessage((data) => { console.log("echo:", data); ws.close(); });
ws.send("hello-from-s2script");
Back to modules All packages

s2script — Source 2 plugin framework

GitHub