import { SseData } from '../types/entity'; import type { ConnectionClosedPayload } from '../types/entity'; interface WebSocketOptions { url: string; token: string; retryInterval?: number; connectTimeout?: number; maxRetries?: number; debug?: boolean; onMessage?: (response: SseData) => void; onReconnectSuccess?: () => void; onConnectionClosed?: (payload: ConnectionClosedPayload) => void; onMaxRetriesReached?: () => void; onConnectionChange?: (isConnected: boolean) => void; } declare class WebSocketClient { private url; private token; private websocket; private reconnectAttempts; private maxReconnectAttempts; private reconnectTimeout; private connectTimeout; private reconnectTimer; private handlers; private connected; private onMessageCallback?; private onReconnectSuccessCallback?; private onConnectionClosedCallback?; private onMaxRetriesReachedCallback?; private onConnectionChangeCallback?; private connectTimeoutRef; private isDestroyed; private debug; private lastPingTime; private pingCheckTimer; private pageVisibilityHandler; private isPageVisible; private lastVisibleTime; private readonly PING_TIMEOUT; private readonly PING_CHECK_INTERVAL; private readonly RECONNECT_DELAY; private readonly PAGE_HIDDEN_TOLERANCE; constructor(options: WebSocketOptions); private debugLog; private debugError; private debugWarn; subscribe(type: string, handler: Function): void; private addMessageHandler; connect(): Promise; private notifyConnectionClosed; private handleReconnect; /** * 处理二进制消息(与 FRONTEND_PROTOBUF_PARSING_GUIDE_WEB.md 一致:0x01=Protobuf,0x7D=PING,0x7E=PONG) * @param buffer ArrayBuffer 数据 */ private handleBinaryMessage; /** * 处理文本消息 * @param text 文本数据 */ private handleTextMessage; /** * 发送 pong 响应给服务端 * @param timestamp 服务端发送的时间戳 */ private sendPong; /** * 初始化页面可见性检测 * 用于处理页面在后台时定时器被节流的问题 */ private initPageVisibilityDetection; /** * 启动ping超时检测 * 如果长时间未收到服务端ping,则认为连接断开,延迟后重连 */ private startPingTimeoutCheck; /** * 停止ping超时检测 */ private stopPingTimeoutCheck; /** * 延迟重连,防止重连风暴 */ private delayedReconnect; private cleanup; /** * 清理页面可见性检测监听器 */ private cleanupPageVisibilityDetection; unsubscribe(): void; destroyWebSocket(): void; close(): void; isConnected(): boolean; /** * 发送消息到服务器 * @param data 要发送的数据(字符串、对象、ArrayBuffer、Blob 或 ArrayBufferView) * - 字符串:直接发送 → Netty 接收为 TextWebSocketFrame * - 对象:会被序列化为 JSON 字符串 → Netty 接收为 TextWebSocketFrame * - ArrayBuffer/Blob/Uint8Array 等:以二进制形式发送 → Netty 接收为 BinaryWebSocketFrame * @returns 是否发送成功 */ send(data: string | object | ArrayBuffer | Blob | ArrayBufferView): boolean; } export default WebSocketClient;