import { i as RetryOptions, t as EventEmitter } from "./emitter-yGbfYzOM.js"; //#region src/core/websocket-client.d.ts /** * Configuration for {@link WebSocketClient}. * * @example * const ws = new WebSocketClient({ * url: 'wss://api.example.com/ws', * reconnect: { maxRetries: 5, initialDelayMs: 1000, backoffFactor: 2 }, * heartbeat: { interval: 30_000, message: 'ping', timeout: 5_000 }, * }); * ws.on('message', (e) => console.log(JSON.parse(e.data))); */ interface WebSocketConfig { /** The WebSocket server URL (must start with `ws://` or `wss://`). */ url: string; /** * One or more sub-protocol names passed to the `WebSocket` constructor. * The server selects which protocol to use. * @example 'json' or ['v1.protocol', 'v2.protocol'] */ protocols?: string | string[]; /** * Automatic reconnect behaviour when the connection is lost. * Pass `true` for defaults (5 retries, exponential back-off starting at 1s) * or a `RetryOptions` object to customise the delay and attempt count. * @default false */ reconnect?: boolean | RetryOptions; /** * Heartbeat / keep-alive configuration. * A ping message is sent at `interval` ms. If no server response arrives * within `timeout` ms, the socket is closed (triggering reconnect if enabled). */ heartbeat?: { /** How often to send the ping message, in milliseconds. */ interval: number; /** * Message payload sent as the heartbeat ping. * Objects are automatically JSON-serialised. * @default 'ping' */ message?: string | object; /** * Milliseconds to wait for a response before closing the socket. * When omitted, no timeout is applied. */ timeout?: number; }; /** * Whether to open the WebSocket connection immediately when the client * is constructed. Set to `false` to defer until `connect()` is called. * @default true */ autoConnect?: boolean; } /** * Events emitted by {@link WebSocketClient}. * * @example * ws.on('open', (e) => console.log('Connected')); * ws.on('close', (e) => console.log('Disconnected:', e.code)); * ws.on('error', (e) => console.error('Error', e)); * ws.on('message', (e) => console.log('Data:', e.data)); * ws.on('reconnect', (n) => console.log(`Attempt #${n}`)); * ws.on('reconnect:fail',(err) => console.error('Gave up:', err)); */ type WebSocketEvents = { open: [Event]; close: [CloseEvent]; error: [Event]; message: [MessageEvent]; /** Emitted before each reconnect attempt with the current attempt number. */ reconnect: [number]; /** Emitted when all reconnect attempts are exhausted. */ 'reconnect:fail': [Error]; }; declare class WebSocketClient extends EventEmitter { private config; private ws; private reconnectAttempts; private isExplicitlyClosed; private heartbeatTimer; private heartbeatTimeoutTimer; constructor(config: WebSocketConfig); connect(): void; send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void; sendJson(data: T): void; close(code?: number, reason?: string): void; private setupListeners; private handleReconnect; private handleConnectionError; private startHeartbeat; private stopHeartbeat; private resetHeartbeatTimeout; } //#endregion export { WebSocketConfig as n, WebSocketEvents as r, WebSocketClient as t }; //# sourceMappingURL=websocket-client-B_Z23trM.d.ts.map