/** * @module SimpleWebSocket * WebSocket client with automatic reconnection and message queuing. * Provides a reliable messaging layer over WebSocket connections. * * @example * // Create and connect * const ws = new WebSocketClient('wss://chat.example.com'); * ws.connect(); * * // Send and receive * await ws.send({ text: 'Hello' }); * const msg = await ws.receive(); */ /** * Simplified message event for WebSocket data. */ export interface SimpleDataEvent { data: string | ArrayBufferLike | Blob | ArrayBufferView; } /** * Abstraction interface for WebSocket to enable unit testing. * Implement this for custom WebSocket instances or mocks. */ export interface WebSocketAbstraction { onopen: ((event: Event) => void) | null; onerror: ((event: ErrorEvent) => void) | null; onclose: ((event: CloseEvent) => void) | null; onmessage: ((event: SimpleDataEvent) => void) | null; send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void; close(): void; } /** * Factory function type for creating WebSocket instances. */ export type WebSocketFactory = () => WebSocketAbstraction; /** * Managed WebSocket client with automatic reconnection and message queuing. * * Features: * - Automatic reconnection on disconnect * - Message queuing when disconnected * - Type-safe message handling with optional codecs * - Promise-based receive API * * @template TMessage - The type of messages sent and received * * @example * // Basic usage * interface ChatMessage { user: string; text: string; } * * const client = new WebSocketClient('wss://chat.example.com'); * client.connect(); * * // Send messages * await client.send({ user: 'John', text: 'Hello!' }); * * // Receive messages * while (true) { * const message = await client.receive(); * console.log(`${message.user}: ${message.text}`); * } * * @example * // With options * const client = new WebSocketClient('wss://api.example.com', { * autoReconnect: true, * onConnect: (socket) => console.log('Connected'), * onClose: () => console.log('Disconnected') * }); */ export declare class WebSocketClient { private options?; private ws?; private receiveQueue; private receivePromiseWrapper?; private sendQueue; private _isConnected; private isSendingQueue; private url?; private wsFactory?; private reconnectAttempts; private shouldReconnect; get connected(): boolean; constructor(urlOrWebSocketFactory: string | WebSocketFactory, options?: WebSocketOptions | undefined); connect(): void; disconnect(): void; send(data: TMessage): void; /** * Receive a new message. * * @throws Error if called while another receive() is pending. * @throws Error if connection closes while waiting. */ receive(): Promise; private onMessage; private reConnect; private sendInternal; private sendQueueItems; } /** * CODEC used for messages. */ export interface WebSocketCodec { /** * * @param data */ encode(data: TMessage): string | ArrayBufferLike | Blob | ArrayBufferView; /** * * @param data */ decode(data: string | ArrayBufferLike | Blob | ArrayBufferView): TMessage; } /** * Configuration options for @see WebSocketClient. */ export interface WebSocketOptions { /** * CODEC to use for inbound and outbound messages (if something else that JSON should be used). */ codec?: WebSocketCodec; /** * Automatically reconnect when getting disconnected (default: true). */ autoReconnect?: boolean; /** * Initial delay in milliseconds before reconnecting (default: 1000). * Uses exponential backoff on subsequent attempts. */ reconnectDelay?: number; /** * Maximum delay in milliseconds between reconnect attempts (default: 30000). */ maxReconnectDelay?: number; /** * Callback when the WS is connected. * * Can be used for authentication messages etc. * * @param socket Socket */ onConnect?: (socket: WebSocketClient) => void; /** * Invoked when the connection is closed. * * The connection will be automatically reconnected if configured (on by default). * * @param socket Socket. */ onClose?: (socket: WebSocketClient) => void; }