import type { SocketConfig, SocketMessageInterface } from '../interface/socket'; /** * Socket 类 * 用于管理 socket 链接,以及 socket 的 ping pong 逻辑,后续可以迁移到 SSE * * @export * @class Socket */ export default class Socket { /** * 用于 socket 的参数配置 * * @private * @type {SocketConfig} * @memberof Socket */ private _config; /** * 用于管理 socket 的实例 * * @private * @type {WebSocket} * @memberof Socket */ private _webSocket?; /** * 用于管理 socket 的配置信息 * * @private * @type {WebSocketConfigModel} * @memberof Socket */ private _webSocketData?; /** * 用于在 ws 的时候出现异常的时候(比如说后端 WS 服务挂了)重试上限 * * @private * @memberof Socket */ private _retryCount; /** * 处理心跳逻辑 */ private _heartbeatManager?; /** * Websocket 生命周期 */ private _eventManager?; /** * 遇到ws断开重连的时候,用于标记是否正在重连,与最开始的新建socket区分开 */ private _isChangingSocket; /** getter 方法 */ /** * socket 配置的 url * * @readonly * @private * @type {SocketURLConfig} * @memberof Socket */ private get _socketUrlConfig(); /** * socket 生命周期,方便内部调用 * * @readonly * @private * @type {(WebSocketLifeCycle | undefined)} * @memberof Socket */ private get _lifeCycle(); private get _socketLifeCycle(); /** * socket 的 log,方便内部调用 * * @readonly * @private * @type {(LogFunc | undefined)} * @memberof Socket */ private get _log(); /** * socket 的 wsId * * @readonly * @type {string} * @memberof Socket */ get wsId(): string; /** * socket 链接的 url 地址 * * @readonly * @private * @type {string} * @memberof Socket */ private get _address(); get isChangingSocket(): boolean; constructor(data: SocketConfig); private _isSetupSocket; /************** PUBLIC METHOD **************/ /** * 创建 Socket * @returns */ build: () => Promise; /** * 关闭 socket * * @memberof Socket */ close(): void; /** * 发送消息 * * @param {SocketMessageInterface} data * @memberof Socket */ sendMsg: (data: SocketMessageInterface, resend?: boolean) => Promise; isOpen: () => boolean; /** * 清空重试次数 */ clearRetryCount: () => void; /** * 发送 Ack 消息 * @param seqId seqId */ sendAck(seqId: string): void; /** * 创建长链接 * @returns WS 的配置信息 */ private _createWebsocket; /** * 多次重试创建 websocket,避免因为网络问题导致的 websocket 创建失败,这里仅是为了找后端获取对应的 socket 字段。 * @returns 初始化 websocket 配置,如果失败则重试 */ private _retryCreateWebSocket; /** * 创建 websocket,配置相关回调,开启心跳 * @returns 创建 websocket */ private _buildSocket; }