UNPKG

@colyseus/ws-transport

Version:

```typescript import { Server } from "@colyseus/core"; import { WebSocketTransport } from "@colyseus/ws-transport";

55 lines (54 loc) 2.2 kB
import http from 'http'; import WebSocket, { type ServerOptions, WebSocketServer } from 'ws'; import express from 'express'; import { Transport } from '@colyseus/core'; type RawWebSocketClient = WebSocket & { pingCount: number; }; export interface TransportOptions extends ServerOptions { pingInterval?: number; pingMaxRetries?: number; } /** * Options for binding this transport to an existing HTTP server. * * This is primarily used by `colyseus/vite`, which shares Vite's dev HTTP server * and forwards only Colyseus websocket upgrade requests to this transport. */ export interface AttachToServerOptions { /** * Return `true` to let this transport handle the upgrade request. * Requests that return `false` are left for the host HTTP server. */ filter?: (req: http.IncomingMessage) => boolean; } export declare class WebSocketTransport extends Transport { protected wss: WebSocketServer; protected pingInterval: NodeJS.Timeout; protected pingIntervalMS: number; protected pingMaxRetries: number; protected shouldShutdownServer: boolean; private _originalSend; private _expressApp?; constructor(options?: TransportOptions); getExpressApp(): express.Application; listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): this; /** * Attach this transport to an already-running HTTP server. * * `colyseus/vite` uses this in dev mode so Colyseus can reuse Vite's HTTP server * instead of creating and owning a separate one. */ attachToServer(server: http.Server, options?: AttachToServerOptions): this; /** * Close the websocket server and all active websocket connections. * * When attached through `attachToServer()`, keep the shared HTTP server alive. * This is required for `colyseus/vite`, which does not own the Vite dev server. */ shutdown(): void; simulateLatency(milliseconds: number): void; protected autoTerminateUnresponsiveClients(pingInterval: number, pingMaxRetries: number): void; protected onConnection(rawClient: RawWebSocketClient, req: http.IncomingMessage): Promise<void>; } export {};