Skip to content
BasaltSoftware
Bedrock 1.26.60

Site colour

Discord
Esc

    ↑↓ moveEnter openEsc close

    Keyboard shortcuts
    Ctrl K
    Search the whole site, from any page
    /
    Jump to the sidebar search, or search the whole site where there isn't one
    Enter
    Open the top search result
    Esc
    Clear the search, or close this panel
    ?
    Show these shortcuts

    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(url: string, options?: WebSocketOptions);

    Starts opening a socket to url.

    Parameters
    url
    string
    optionsoptional
    WebSocketOptions
    Throws
    • SyntaxError if url isn’t a ws or wss URL, and SecurityError if basalt.toml doesn’t allow its host.

    static read-only

    static readonly CONNECTING: 0;

    static read-only

    static readonly OPEN: 1;

    static read-only

    static readonly CLOSING: 2;

    static read-only

    static readonly CLOSED: 3;

    read-only

    readonly url: string;

    The URL it was opened with.

    read-only

    readonly readyState: 0 | 1 | 2 | 3;

    WebSocket.CONNECTING, OPEN, CLOSING or CLOSED.

    read-only

    readonly protocol: string;

    The subprotocol the server chose, empty for none.

    onopen: ((event: WebSocketOpenEvent) => void) | null;
    onmessage: ((event: WebSocketMessageEvent) => void) | null;
    onclose: ((event: WebSocketCloseEvent) => void) | null;
    onerror: ((event: WebSocketErrorEvent) => void) | null;
    send(data: string): void;

    Sends a text message. Does nothing once closing.

    Parameters
    data
    string
    Throws
    • InvalidStateError while still connecting.
    close(code?: number, reason?: string): void;

    Starts closing, waiting for the server’s goodbye before onclose.

    Parameters
    codeoptional
    number1000, the default, or 3000 to 4999.
    reasonoptional
    string
    Throws
    • InvalidAccessError for any other code.
    addEventListener<K extends keyof WebSocketEventMap>(
    type: K,
    listener: (event: WebSocketEventMap[K]) => void,
    ): void;
    Parameters
    type
    K
    listener
    (event: WebSocketEventMap[K]) => void
    removeEventListener<K extends keyof WebSocketEventMap>(
    type: K,
    listener: (event: WebSocketEventMap[K]) => void,
    ): void;
    Parameters
    type
    K
    listener
    (event: WebSocketEventMap[K]) => void

    WebSocketMessageEvent.target, WebSocketCloseEvent.target, WebSocketErrorEvent.target, WebSocketOpenEvent.target