{"version":3,"file":"WebSocketTransport.mjs","sources":["../../src/transport/WebSocketTransport.ts"],"sourcesContent":["import NodeWebSocket from \"ws\";\nimport { CloseCode } from \"@colyseus/shared-types\";\nimport type { ITransport, ITransportEventMap } from \"./ITransport.ts\";\n\nconst WebSocket = globalThis.WebSocket || NodeWebSocket;\n\n// Once per process — the fallback is per-send, and a 60Hz input ring would\n// otherwise flood the console.\nlet warnedNoUnreliableChannel = false;\n\nexport class WebSocketTransport implements ITransport {\n    ws: WebSocket | NodeWebSocket;\n    protocols?: string | string[];\n\n    events: ITransportEventMap;\n\n    constructor(events: ITransportEventMap) {\n        this.events = events;\n    }\n\n    public send(data: Buffer | Uint8Array): void {\n        this.ws.send(data);\n    }\n\n    /**\n     * WebSocket has no unreliable channel, so this falls back to the reliable\n     * one — which is what every caller is written against (see `sendRequest`,\n     * and the input handle's redundancy ring). Dropping instead loses every\n     * `mode:\"unreliable\"` input and request silently: the server never sees\n     * them, and the client's pending set grows without bound because nothing\n     * is ever acked.\n     *\n     * The cost of falling back is bandwidth, not correctness — an input ring\n     * carries `historySize` redundant slots the ordered channel doesn't need,\n     * and the server dedupes them by wire seq exactly as it would over\n     * datagrams. Use `@colyseus/h3-transport` for real unreliable delivery.\n     */\n    public sendUnreliable(data: Buffer | Uint8Array): void {\n        if (!warnedNoUnreliableChannel) {\n            warnedNoUnreliableChannel = true;\n            console.warn(\"@colyseus/sdk: the WebSocket transport has no unreliable channel — sending `mode:\\\"unreliable\\\"` traffic reliably instead. Use @colyseus/h3-transport (WebTransport) for datagram delivery.\");\n        }\n        this.send(data);\n    }\n\n    /**\n     * @param url URL to connect to\n     * @param headers custom headers to send with the connection (only supported in Node.js. Web Browsers do not allow setting custom headers)\n     */\n    public connect(url: string, headers?: any): void {\n        if (typeof document !== \"undefined\") {\n            // browser: the options object would be read as an invalid subprotocol,\n            // which Safari logs to the console even though the throw is caught\n            this.ws = new WebSocket(url, this.protocols);\n\n        } else {\n            try {\n                // Node or Bun environments (supports custom headers)\n                this.ws = new WebSocket(url, { headers, protocols: this.protocols });\n\n            } catch (e) {\n                // runtimes without the options form\n                this.ws = new WebSocket(url, this.protocols);\n            }\n        }\n\n        const ws = this.ws;\n        let opened = false;\n        let closed = false;\n\n        const onclose = (event: any) => {\n            if (closed) { return; }\n            closed = true;\n            this.events.onclose?.(event);\n        };\n\n        ws.binaryType = 'arraybuffer';\n        ws.onopen = (event) => {\n            opened = true;\n            this.events.onopen?.(event);\n        };\n        ws.onmessage = (event) => this.events.onmessage?.(event);\n        ws.onclose = onclose;\n        ws.onerror = (event) => {\n            // undici (Node 22's global WebSocket) fires no close for a socket that never opened;\n            // browsers and `ws` have closed it by the next tick. Scheduled first: a listener may throw.\n            if (!opened) {\n                // forwarded once: undici fires error again from every close() while still CONNECTING\n                ws.onerror = null;\n\n                setTimeout(() => {\n                    // close(MAY_TRY_RECONNECT) detaches the handler and reports the close itself\n                    if (ws.onclose === onclose) { onclose({ code: CloseCode.ABNORMAL_CLOSURE, reason: \"\" }); }\n                }, 0);\n            }\n\n            this.events.onerror?.(event);\n        };\n    }\n\n    public close(code?: number, reason?: string) {\n        //\n        // trigger the onclose event immediately if the code is MAY_TRY_RECONNECT\n        // when \"offline\" event is triggered, the close frame is delayed. this\n        // way client can try to reconnect immediately.\n        //\n        try {\n            if (code === CloseCode.MAY_TRY_RECONNECT && this.events.onclose) {\n                this.ws.onclose = null;\n                this.events.onclose({ code, reason });\n            }\n\n        } finally {\n            // then we close the connection, even if a room listener (onDrop, onLeave) threw\n            this.ws.close(code, reason);\n        }\n    }\n\n    get isOpen() {\n        return this.ws.readyState === WebSocket.OPEN;\n    }\n\n}\n"],"names":[],"mappings":";;;;;;;;;AAIA,MAAM,SAAS,GAAG,UAAU,CAAC,SAAS,IAAI,aAAa;AAEvD;AACA;AACA,IAAI,yBAAyB,GAAG,KAAK;MAExB,kBAAkB,CAAA;AAC3B,IAAA,EAAE;AACF,IAAA,SAAS;AAET,IAAA,MAAM;AAEN,IAAA,WAA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