UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

49 lines 1.89 kB
/** * Transport adapter over a `WebSocket`. * * **Not** the right choice for game state — WebSocket runs over TCP, which * means head-of-line blocking under packet loss. Use {@link WebRTCDataChannelTransport} * for game state. WebSocket is appropriate for lobby/matchmaking, chat, replay * uploads, and other use cases where ordering and reliability matter more * than tail latency. * * Duck-typed against the standard browser `WebSocket` interface so tests can * pass a mock. The expected interface: * - `binaryType` (writable; we set it to `'arraybuffer'`) * - `addEventListener(type, handler)` for `'message'`, `'close'`, `'error'`, `'open'` * - `send(ArrayBufferView | ArrayBuffer)` * - `close()` * - `readyState` * * Either pass an already-constructed WebSocket via `socket`, or pass a `url` * and let the adapter construct one (browser only — `WebSocket` must be available). * * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class WebSocketTransport extends Transport { /** * @param {{ socket?: WebSocket, url?: string }} options */ constructor({ socket, url }?: { socket?: WebSocket; url?: string; }); /** @type {WebSocket} */ socket: WebSocket; /** * Resolve when the socket is OPEN. If already open, resolves immediately. * Useful when constructed with a `url` and the caller wants to await readiness. * * Rejects on either `'error'` or `'close'` — some failure modes (server * rejects the handshake with a clean close, network drops mid-handshake) * fire `'close'` without `'error'`; listening on only one would hang the * promise forever. * * @returns {Promise<void>} */ connect(): Promise<void>; #private; } import { Transport } from "../Transport.js"; //# sourceMappingURL=WebSocketTransport.d.ts.map