Basalt's own, as the browser's WebSocket: a ws or wss connection, text messages each way.
Class in @minecraft/server-net
Basalt’s own, as the browser’s WebSocket: a ws or wss connection, text messages each way.
Mojang’s @minecraft/server-net has none. Messages arrive between the plugin’s other work,
and a plugin’s sockets close (code 1001) when it’s disposed or reloaded.
const ws = new WebSocket("wss://example.org/feed", { headers: { Authorization: "Bearer x" } });ws.onopen = () => ws.send("hello");ws.onmessage = (event) => world.sendMessage(event.data);ws.onclose = (event) => console.warn(`closed ${event.code} ${event.reason}`);import { WebSocket } from '@minecraft/server-net';Constructor
Section titled “Constructor”constructor(url: string, options?: WebSocketOptions);Starts opening a socket to url.
urlstringoptionsoptionalWebSocketOptions
- SyntaxError if
urlisn’t awsorwssURL, and SecurityError if basalt.toml doesn’t allow its host.
Properties
Section titled “Properties”CONNECTING
Section titled “CONNECTING”static read-only
static readonly CONNECTING: 0;static read-only
static readonly OPEN: 1;CLOSING
Section titled “CLOSING”static read-only
static readonly CLOSING: 2;CLOSED
Section titled “CLOSED”static read-only
static readonly CLOSED: 3;read-only
readonly url: string;The URL it was opened with.
readyState
Section titled “readyState”read-only
readonly readyState: 0 | 1 | 2 | 3;WebSocket.CONNECTING, OPEN, CLOSING or CLOSED.
protocol
Section titled “protocol”read-only
readonly protocol: string;The subprotocol the server chose, empty for none.
onopen
Section titled “onopen”onopen: ((event: WebSocketOpenEvent) => void) | null;onmessage
Section titled “onmessage”onmessage: ((event: WebSocketMessageEvent) => void) | null;onclose
Section titled “onclose”onclose: ((event: WebSocketCloseEvent) => void) | null;onerror
Section titled “onerror”onerror: ((event: WebSocketErrorEvent) => void) | null;Methods
Section titled “Methods”send(data: string): void;Sends a text message. Does nothing once closing.
datastring
- InvalidStateError while still connecting.
close(code?: number, reason?: string): void;Starts closing, waiting for the server’s goodbye before onclose.
codeoptionalnumber1000, the default, or 3000 to 4999.reasonoptionalstring
- InvalidAccessError for any other code.
addEventListener
Section titled “addEventListener”addEventListener<K extends keyof WebSocketEventMap>( type: K, listener: (event: WebSocketEventMap[K]) => void,): void;typeKlistener(event: WebSocketEventMap[K]) => void
removeEventListener
Section titled “removeEventListener”removeEventListener<K extends keyof WebSocketEventMap>( type: K, listener: (event: WebSocketEventMap[K]) => void,): void;typeKlistener(event: WebSocketEventMap[K]) => void
Used by
Section titled “Used by”WebSocketMessageEvent.target, WebSocketCloseEvent.target, WebSocketErrorEvent.target, WebSocketOpenEvent.target