import { Subscribe } from './subscribe'; /** * 鲁棒ws的配置参数 */ export type RobustWebSocketConstructorOptions = { url: string | ((ws: RobustWebSocket) => string); protocols?: string[]; robustOptions?: RobustOptions; }; /** * ws 鲁棒性的配置 */ export type RobustOptions = { timeout?: number; shouldReconnect?: (event: CloseEvent, ws: RobustWebSocket) => undefined | number; should1000Reconnect?: boolean; ignoreConnectivityEvents?: boolean; automaticOpen?: boolean; }; /** * 重连的事件详情 */ export type RobustWebSocketEventDetail = { reconnects: number; attempts: number; }; export type RobustWebSocketEvent = Event & { detail: T; }; export type RobustWebSocketEventMap = { connecting: (event: RobustWebSocketEvent) => void; timeout: (event: RobustWebSocketEvent) => void; open: (event: RobustWebSocketEvent) => void; closing: () => void; close: (event: CloseEvent) => void; error: (event: Event) => void; message: (event: MessageEvent) => void; }; /** * 具有鲁棒性的WebSocket封装 * 1、自动重连 * 2、可根据网络情况进行重连 */ export declare class RobustWebSocket extends Subscribe { private _realWebSocket; private _url?; private _protocols; private _readyState; private _options; private _connectTimeout; private _attempts; private _reconnects; private _reconnectWhenOnlineAgain; _explicitlyClosed: boolean; private _pendingReconnect; private _connectivityEventsAttached; private _heathCheckTimer; get attempts(): number; constructor(options?: RobustWebSocketConstructorOptions); setOptions(options: RobustWebSocketConstructorOptions): void; /** * 做一个健康检查 */ private _healthCheck; /** * 清理超时计时器 */ private _clearConnectTimeoutIfNeed; /** * 清理健康检查 */ private _clearHeathCheckIfNeed; /** * 开启健康检查 */ private _startHealthCheck; /** * 停止健康检查 */ private _stopHealthCheck; /** * 清理连接中的请求 */ private _clearPendingReconnectIfNeeded; /** * 当网页连接上网络进行重连 * @param event */ private _online; /** * 当网页断开网络进行关闭 */ private _offline; /** * 解除网络监听事件 */ private _detachConnectivityEvents; /** * 注册网络监听事件 */ private _attachConnectivityEvents; /** * 发送消息 * @param data * @returns */ send: (data: string | ArrayBuffer | Blob | ArrayBufferView) => void | undefined; /** * 关闭连接 * @param code 代码 * @param reason 原因 * @returns */ close: (code?: number | string, reason?: string) => void | undefined; /** * 进行ws连接 */ open: (url?: string) => void; /** * 创建ws连接 */ private _createWebSocket; /** * 断开ws事件 */ private _removeWebSocketEvent; /** * 分发ws事件 */ private _dispatchWebSocketEvent; /** * ws连接事件 * @returns */ private _webSocketOpenHandler; /** * ws 关闭事件 * @param event * @returns */ private _webSocketCloseHandler; /** * ws 异常 * @param event * @returns */ private _webSocketErrorHandler; /** * ws消息 * @param event * @returns */ private _webSocketMessageHandler; /** * 进行重连 * @param event * @returns */ private _reconnect; }