a2bei4-utils 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +2 -2
- package/dist/a2bei4.utils.cjs.js +1051 -250
- package/dist/a2bei4.utils.cjs.js.map +1 -1
- package/dist/a2bei4.utils.cjs.min.js +1 -1
- package/dist/a2bei4.utils.cjs.min.js.map +1 -1
- package/dist/a2bei4.utils.esm.js +1047 -251
- package/dist/a2bei4.utils.esm.js.map +1 -1
- package/dist/a2bei4.utils.esm.min.js +1 -1
- package/dist/a2bei4.utils.esm.min.js.map +1 -1
- package/dist/a2bei4.utils.umd.js +1051 -250
- package/dist/a2bei4.utils.umd.js.map +1 -1
- package/dist/a2bei4.utils.umd.min.js +1 -1
- package/dist/a2bei4.utils.umd.min.js.map +1 -1
- package/dist/arr.cjs +27 -27
- package/dist/arr.cjs.map +1 -1
- package/dist/arr.js +27 -27
- package/dist/arr.js.map +1 -1
- package/dist/audio.cjs +281 -0
- package/dist/audio.cjs.map +1 -0
- package/dist/audio.js +278 -0
- package/dist/audio.js.map +1 -0
- package/dist/common.cjs +6 -6
- package/dist/common.cjs.map +1 -1
- package/dist/common.js +6 -6
- package/dist/common.js.map +1 -1
- package/dist/download.cjs +43 -0
- package/dist/download.cjs.map +1 -1
- package/dist/download.js +43 -1
- package/dist/download.js.map +1 -1
- package/dist/evt.cjs +148 -148
- package/dist/evt.cjs.map +1 -1
- package/dist/evt.js +148 -148
- package/dist/evt.js.map +1 -1
- package/dist/id.cjs +68 -68
- package/dist/id.cjs.map +1 -1
- package/dist/id.js +68 -68
- package/dist/id.js.map +1 -1
- package/dist/timer.cjs +0 -1
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.js +0 -1
- package/dist/timer.js.map +1 -1
- package/dist/tree.cjs +75 -0
- package/dist/tree.cjs.map +1 -1
- package/dist/tree.js +75 -1
- package/dist/tree.js.map +1 -1
- package/dist/webSocket.cjs +409 -0
- package/dist/webSocket.cjs.map +1 -0
- package/dist/webSocket.js +407 -0
- package/dist/webSocket.js.map +1 -0
- package/package.json +12 -1
- package/readme.txt +8 -5
- package/types/audio.d.ts +57 -0
- package/types/download.d.ts +12 -1
- package/types/index.d.ts +208 -2
- package/types/tree.d.ts +17 -1
- package/types/webSocket.d.ts +124 -0
package/dist/tree.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tree.js","sources":["../src/source/tree.js"],"sourcesContent":["/**\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {T[]} data - 嵌套树森林\n * @param {string} [idKey='id'] - 主键字段\n * @param {string} [childrenKey='children'] - 子节点字段\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\n */\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\n const retObj = {};\n function fn(nodes) {\n if (Array.isArray(nodes) && nodes.length > 0) {\n nodes.forEach((node) => {\n retObj[node[idKey]] = { ...node };\n retObj[node[idKey]][childrenKey] = null;\n\n fn(node[childrenKey]);\n });\n }\n }\n fn(data);\n return retObj;\n}\n\n/**\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\n * @param {number | string} [parentId=0] - 根节点标识值\n * @param {Object} [opts] - 字段映射配置\n * @param {string} [opts.idKey='id'] - 节点主键\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\n */\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\n const map = new Map(); // id -> node\n const items = []; // 多根森林\n\n // 1. 初始化:保证每个节点都有 children,并存入 map\n for (const item of nodes) {\n const node = { ...item, [childrenKey]: [] };\n map.set(item[idKey], node);\n }\n\n // 2. 建立父子关系\n for (const item of nodes) {\n const node = map.get(item[idKey]);\n const parentIdVal = item[parentKey];\n\n if (parentIdVal === parentId) {\n // 根层\n items.push(node);\n } else {\n // 非根层:找到父节点,把自己挂上去\n const parent = map.get(parentIdVal);\n if (parent) parent[childrenKey].push(node);\n // 如果 parent 不存在,说明数据不完整,可自定义处理\n }\n }\n\n return items;\n}\n\n/**\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {string | number} id - 要查找的 id\n * @param {T[]} arr - 嵌套树森林\n * @param {string} [resultKey='name'] - 需要返回的字段\n * @param {string} [idKey='id'] - 主键字段\n * @param {string} [childrenKey='children'] - 子节点字段\n * @returns {any} 找到的值;未找到返回 `undefined`\n */\nexport const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\n if (Array.isArray(arr) && arr.length > 0) {\n for (let i = 0; i < arr.length; i++) {\n const item = arr[i];\n if (item[idKey]?.toString() === id?.toString()) {\n return item[resultKey];\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\n const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);\n if (result) {\n return result;\n }\n }\n }\n }\n};\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE;AACrB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,C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,"file":"tree.js","sources":["../src/source/tree.js"],"sourcesContent":["/**\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {T[]} data - 嵌套树森林\n * @param {string} [idKey='id'] - 主键字段\n * @param {string} [childrenKey='children'] - 子节点字段\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\n */\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\n const retObj = {};\n function fn(nodes) {\n if (Array.isArray(nodes) && nodes.length > 0) {\n nodes.forEach((node) => {\n retObj[node[idKey]] = { ...node };\n retObj[node[idKey]][childrenKey] = null;\n\n fn(node[childrenKey]);\n });\n }\n }\n fn(data);\n return retObj;\n}\n\n/**\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\n * @param {number | string} [parentId=0] - 根节点标识值\n * @param {Object} [opts] - 字段映射配置\n * @param {string} [opts.idKey='id'] - 节点主键\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\n */\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\n const map = new Map(); // id -> node\n const items = []; // 多根森林\n\n // 1. 初始化:保证每个节点都有 children,并存入 map\n for (const item of nodes) {\n const node = { ...item, [childrenKey]: [] };\n map.set(item[idKey], node);\n }\n\n // 2. 建立父子关系\n for (const item of nodes) {\n const node = map.get(item[idKey]);\n const parentIdVal = item[parentKey];\n\n if (parentIdVal === parentId) {\n // 根层\n items.push(node);\n } else {\n // 非根层:找到父节点,把自己挂上去\n const parent = map.get(parentIdVal);\n if (parent) parent[childrenKey].push(node);\n // 如果 parent 不存在,说明数据不完整,可自定义处理\n }\n }\n\n return items;\n}\n\n/**\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\n *\n * @template T extends Record<PropertyKey, any>\n * @param {string | number} id - 要查找的 id\n * @param {T[]} arr - 嵌套树森林\n * @param {string} [resultKey='name'] - 需要返回的字段\n * @param {string} [idKey='id'] - 主键字段\n * @param {string} [childrenKey='children'] - 子节点字段\n * @returns {any} 找到的值;未找到返回 `undefined`\n */\nexport const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\n if (Array.isArray(arr) && arr.length > 0) {\n for (let i = 0; i < arr.length; i++) {\n const item = arr[i];\n if (item[idKey]?.toString() === id?.toString()) {\n return item[resultKey];\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\n const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);\n if (result) {\n return result;\n }\n }\n }\n }\n};\n\n/**\n * 从服务端返回的已选 id 数组里,提取出\n * 1. 叶子节点\n * 2. 所有子节点都被选中的父节点\n * 其余父节点一律丢弃(由前端 Tree 自动算半选)\n *\n * @param {Array} treeData 完整树\n * @param {Array} selectedKeys 后端给的选中 id 数组\n * @param {String} idKey 节点唯一字段\n * @param {String} childrenKey 子节点字段\n * @returns {{checked: string[], halfChecked: string[]}}\n */\nexport function extractFullyCheckedKeys(treeData, selectedKeys, idKey = \"id\", childrenKey = \"children\") {\n const selectedSet = new Set(selectedKeys);\n const checked = new Set();\n const halfChecked = new Set();\n\n /* 返回值含义\n 0 - 未选中\n 1 - 半选\n 2 - 全选\n */\n function dfs(node) {\n const nodeId = node[idKey];\n const children = node[childrenKey] || [];\n\n // 叶子\n if (!children.length) {\n if (selectedSet.has(nodeId)) {\n checked.add(nodeId);\n return 2;\n }\n return 0;\n }\n\n // 非叶子\n let allChecked = true;\n let someChecked = false;\n\n children.forEach((child) => {\n const childState = dfs(child);\n if (childState !== 2) allChecked = false;\n if (childState >= 1) someChecked = true;\n });\n\n // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选\n if (selectedSet.has(nodeId)) {\n if (allChecked) {\n checked.add(nodeId);\n return 2;\n }\n halfChecked.add(nodeId);\n return 1;\n }\n\n // 当前节点不在 selectedKeys 里,看子节点\n if (allChecked) {\n checked.add(nodeId);\n return 2;\n }\n if (someChecked) {\n halfChecked.add(nodeId);\n return 1;\n }\n return 0;\n }\n\n treeData.forEach(dfs);\n return {\n checked: [...checked],\n halfChecked: [...halfChecked]\n };\n}\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE;AACrB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK;AACpC,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE;AACjD,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI;;AAEvD,gBAAgB,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AACrC,YAAY,CAAC,CAAC;AACd,QAAQ;AACR,IAAI;AACJ,IAAI,EAAE,CAAC,IAAI,CAAC;AACZ,IAAI,OAAO,MAAM;AACjB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,2BAA2B,CAAC,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,EAAE,KAAK,GAAG,IAAI,EAAE,SAAS,GAAG,UAAU,EAAE,WAAW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AW;AACpC,KAAK;AACL;;;;"}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @class WebSocketManager - 一个纯粹、强大的 WebSocket 连接管理引擎
|
|
5
|
+
*
|
|
6
|
+
* @features
|
|
7
|
+
* - 智能断线重连 (指数退避算法)
|
|
8
|
+
* - 网络状态监听
|
|
9
|
+
* - 高度可定制的心跳保活机制 (通过注入函数实现)
|
|
10
|
+
* - 页面可见性 API 集成
|
|
11
|
+
* - 消息发送队列
|
|
12
|
+
* - 清晰的生命周期管理
|
|
13
|
+
* - 优雅的资源销毁
|
|
14
|
+
* - 可配置的数据序列化/反序列化
|
|
15
|
+
* - 纯粹的消息传递,不关心消息内容
|
|
16
|
+
*/
|
|
17
|
+
class WebSocketManager {
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} url - WebSocket 服务器的地址
|
|
20
|
+
* @param {object} [options={}] - 配置选项
|
|
21
|
+
* @param {number} [options.heartbeatInterval=30000] - 心跳间隔 (毫秒)
|
|
22
|
+
* @param {number} [options.heartbeatTimeout=10000] - 心跳超时时间 (毫秒)
|
|
23
|
+
* @param {number} [options.reconnectBaseInterval=1000] - 重连基础间隔 (毫秒)
|
|
24
|
+
* @param {number} [options.maxReconnectInterval=30000] - 最大重连间隔 (毫秒)
|
|
25
|
+
* @param {number} [options.maxReconnectAttempts=Infinity] - 最大重连次数
|
|
26
|
+
* @param {boolean} [options.autoConnect=true] - 是否在实例化后自动连接
|
|
27
|
+
* @param {boolean} [options.serializeData=false] - 发送数据时是否自动序列化为JSON字符串
|
|
28
|
+
* @param {boolean} [options.deserializeData=false] - 接收数据时是否自动反序列化为JSON对象
|
|
29
|
+
* @param {function|null} [options.getPingMessage=null] - 返回要发送的 ping 消息内容的函数。如果为 null,则不发送心跳。
|
|
30
|
+
* @param {function|null} [options.isPongMessage=null] - 判断接收到的消息是否为 pong 的函数。接收 MessageEvent 对象作为参数,返回布尔值。
|
|
31
|
+
* @param {object} [options.protocols] - WebSocket 协议
|
|
32
|
+
*/
|
|
33
|
+
constructor(url, options = {}) {
|
|
34
|
+
this.url = url;
|
|
35
|
+
|
|
36
|
+
// 默认的心跳实现,用于向后兼容
|
|
37
|
+
const defaultGetPingMessage = () => JSON.stringify({ type: "ping" });
|
|
38
|
+
const defaultIsPongMessage = (event) => {
|
|
39
|
+
try {
|
|
40
|
+
const message = JSON.parse(event.data);
|
|
41
|
+
return message.type === "pong";
|
|
42
|
+
} catch (e) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
this.options = {
|
|
48
|
+
heartbeatInterval: 30000,
|
|
49
|
+
heartbeatTimeout: 10000,
|
|
50
|
+
reconnectBaseInterval: 1000,
|
|
51
|
+
maxReconnectInterval: 30000,
|
|
52
|
+
maxReconnectAttempts: Infinity,
|
|
53
|
+
autoConnect: true,
|
|
54
|
+
serializeData: false,
|
|
55
|
+
deserializeData: false,
|
|
56
|
+
getPingMessage: defaultGetPingMessage, // 默认提供 ping 消息生成器
|
|
57
|
+
isPongMessage: defaultIsPongMessage, // 默认提供 pong 消息判断器
|
|
58
|
+
...options
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// WebSocket 实例
|
|
62
|
+
this.ws = null;
|
|
63
|
+
|
|
64
|
+
// 状态管理
|
|
65
|
+
this.readyState = WebSocket.CLOSED;
|
|
66
|
+
this.reconnectAttempts = 0;
|
|
67
|
+
this.forcedClose = false;
|
|
68
|
+
this.isReconnecting = false;
|
|
69
|
+
|
|
70
|
+
// 定时器
|
|
71
|
+
this.heartbeatTimer = null;
|
|
72
|
+
this.heartbeatTimeoutTimer = null;
|
|
73
|
+
this.reconnectTimer = null;
|
|
74
|
+
|
|
75
|
+
// 消息队列
|
|
76
|
+
this.messageQueue = [];
|
|
77
|
+
|
|
78
|
+
// 事件监听器
|
|
79
|
+
this.listeners = new Map();
|
|
80
|
+
|
|
81
|
+
// 绑定方法上下文
|
|
82
|
+
this._onOpen = this._onOpen.bind(this);
|
|
83
|
+
this._onMessage = this._onMessage.bind(this);
|
|
84
|
+
this._onClose = this._onClose.bind(this);
|
|
85
|
+
this._onError = this._onError.bind(this);
|
|
86
|
+
this._handleVisibilityChange = this._handleVisibilityChange.bind(this);
|
|
87
|
+
this._handleOnline = this._handleOnline.bind(this);
|
|
88
|
+
this._handleOffline = this._handleOffline.bind(this);
|
|
89
|
+
|
|
90
|
+
this._setupEventListeners();
|
|
91
|
+
|
|
92
|
+
if (this.options.autoConnect) {
|
|
93
|
+
this.connect();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// --- 公共 API ---
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 连接到 WebSocket 服务器
|
|
101
|
+
*/
|
|
102
|
+
connect() {
|
|
103
|
+
if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.forcedClose = false;
|
|
108
|
+
this._updateReadyState(WebSocket.CONNECTING);
|
|
109
|
+
console.log(`[WS] 正在连接到 ${this.url}...`);
|
|
110
|
+
this._emit("connecting");
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
this.ws = new WebSocket(this.url, this.options.protocols);
|
|
114
|
+
this.ws.onopen = this._onOpen;
|
|
115
|
+
this.ws.onmessage = this._onMessage;
|
|
116
|
+
this.ws.onclose = this._onClose;
|
|
117
|
+
this.ws.onerror = this._onError;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error("[WS] 连接失败:", error);
|
|
120
|
+
this._onError(error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 发送数据
|
|
126
|
+
* @param {string|object|ArrayBuffer|Blob} data - 要发送的数据
|
|
127
|
+
*/
|
|
128
|
+
send(data) {
|
|
129
|
+
if (this.readyState === WebSocket.OPEN) {
|
|
130
|
+
let message;
|
|
131
|
+
// 根据配置决定是否序列化数据
|
|
132
|
+
if (this.options.serializeData) {
|
|
133
|
+
message = JSON.stringify(data);
|
|
134
|
+
} else {
|
|
135
|
+
message = data;
|
|
136
|
+
}
|
|
137
|
+
this.ws.send(message);
|
|
138
|
+
console.log("[WS] 消息已发送:", message);
|
|
139
|
+
} else {
|
|
140
|
+
console.warn("[WS] 连接未打开,消息已加入队列:", data);
|
|
141
|
+
this.messageQueue.push(data);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 主动关闭连接
|
|
147
|
+
* @param {number} [code=1000] - 关闭代码
|
|
148
|
+
* @param {string} [reason=''] - 关闭原因
|
|
149
|
+
*/
|
|
150
|
+
close(code = 1000, reason = "Normal closure") {
|
|
151
|
+
this.forcedClose = true;
|
|
152
|
+
this._stopHeartbeat();
|
|
153
|
+
this._clearReconnectTimer();
|
|
154
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
155
|
+
this.ws.close(code, reason);
|
|
156
|
+
} else {
|
|
157
|
+
this._updateReadyState(WebSocket.CLOSED);
|
|
158
|
+
this._emit("close", { code, reason, wasClean: true });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 彻底销毁实例,清理所有资源
|
|
164
|
+
*/
|
|
165
|
+
destroy() {
|
|
166
|
+
console.log("[WS] 正在销毁实例...");
|
|
167
|
+
this.close(1000, "Instance destroyed");
|
|
168
|
+
this._removeEventListeners();
|
|
169
|
+
this.messageQueue = [];
|
|
170
|
+
this.listeners.clear();
|
|
171
|
+
this.ws = null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 添加事件监听器
|
|
176
|
+
* @param {string} eventName - 事件名 (e.g., 'open', 'message', 'close', 'error', 'reconnect')
|
|
177
|
+
* @param {function} callback - 回调函数
|
|
178
|
+
*/
|
|
179
|
+
on(eventName, callback) {
|
|
180
|
+
if (!this.listeners.has(eventName)) {
|
|
181
|
+
this.listeners.set(eventName, []);
|
|
182
|
+
}
|
|
183
|
+
this.listeners.get(eventName).push(callback);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 移除事件监听器
|
|
188
|
+
* @param {string} eventName - 事件名
|
|
189
|
+
* @param {function} callback - 回调函数
|
|
190
|
+
*/
|
|
191
|
+
off(eventName, callback) {
|
|
192
|
+
if (this.listeners.has(eventName)) {
|
|
193
|
+
const callbacks = this.listeners.get(eventName);
|
|
194
|
+
const index = callbacks.indexOf(callback);
|
|
195
|
+
if (index > -1) {
|
|
196
|
+
callbacks.splice(index, 1);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// --- 内部方法 ---
|
|
202
|
+
|
|
203
|
+
_onOpen(event) {
|
|
204
|
+
console.log("[WS] 连接已建立");
|
|
205
|
+
this._updateReadyState(WebSocket.OPEN);
|
|
206
|
+
this.reconnectAttempts = 0;
|
|
207
|
+
this.isReconnecting = false;
|
|
208
|
+
|
|
209
|
+
// 如果配置了 getPingMessage,则启动心跳
|
|
210
|
+
if (this.options.getPingMessage) {
|
|
211
|
+
this._startHeartbeat();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this._flushMessageQueue();
|
|
215
|
+
this._emit("open", event);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 纯粹的消息处理方法:处理心跳,然后将数据交给使用者
|
|
220
|
+
* @param {MessageEvent} event
|
|
221
|
+
*/
|
|
222
|
+
_onMessage(event) {
|
|
223
|
+
// --- 步骤 1: 使用注入的函数优先处理内部心跳机制 ---
|
|
224
|
+
if (this.options.isPongMessage && this.options.isPongMessage(event)) {
|
|
225
|
+
this._handlePong();
|
|
226
|
+
return; // 是心跳消息,处理完毕,直接返回
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- 步骤 2: 根据用户配置处理业务消息 ---
|
|
230
|
+
if (this.options.deserializeData) {
|
|
231
|
+
try {
|
|
232
|
+
const parsedMessage = JSON.parse(event.data);
|
|
233
|
+
this._emit("message", parsedMessage, event);
|
|
234
|
+
} catch (e) {
|
|
235
|
+
this._emit("message", event.data, event);
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
this._emit("message", event.data, event);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_onClose(event) {
|
|
243
|
+
console.log("[WS] 连接已关闭", event);
|
|
244
|
+
this._updateReadyState(WebSocket.CLOSED);
|
|
245
|
+
this._stopHeartbeat();
|
|
246
|
+
this._emit("close", event);
|
|
247
|
+
|
|
248
|
+
if (!this.forcedClose) {
|
|
249
|
+
this._scheduleReconnect();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
_onError(event) {
|
|
254
|
+
console.error("[WS] 连接发生错误:", event);
|
|
255
|
+
this._emit("error", event);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// --- 心跳机制 ---
|
|
259
|
+
_startHeartbeat() {
|
|
260
|
+
// 如果没有配置 getPingMessage,则无法启动心跳
|
|
261
|
+
if (!this.options.getPingMessage) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
this._stopHeartbeat();
|
|
266
|
+
this.heartbeatTimer = setInterval(() => {
|
|
267
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
268
|
+
try {
|
|
269
|
+
// 调用注入的函数获取消息内容并发送
|
|
270
|
+
const pingMessage = this.options.getPingMessage();
|
|
271
|
+
this.ws.send(pingMessage);
|
|
272
|
+
console.log("[WS] 发送 Ping:", pingMessage);
|
|
273
|
+
this._setHeartbeatTimeout();
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.error("[WS] 发送 Ping 消息失败:", error);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}, this.options.heartbeatInterval);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
_stopHeartbeat() {
|
|
282
|
+
if (this.heartbeatTimer) {
|
|
283
|
+
clearInterval(this.heartbeatTimer);
|
|
284
|
+
this.heartbeatTimer = null;
|
|
285
|
+
}
|
|
286
|
+
this._clearHeartbeatTimeout();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
_setHeartbeatTimeout() {
|
|
290
|
+
this._clearHeartbeatTimeout();
|
|
291
|
+
this.heartbeatTimeoutTimer = setTimeout(() => {
|
|
292
|
+
console.error("[WS] 心跳超时,主动断开连接");
|
|
293
|
+
this.ws.close(1006, "Heartbeat timeout");
|
|
294
|
+
}, this.options.heartbeatTimeout);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
_clearHeartbeatTimeout() {
|
|
298
|
+
if (this.heartbeatTimeoutTimer) {
|
|
299
|
+
clearTimeout(this.heartbeatTimeoutTimer);
|
|
300
|
+
this.heartbeatTimeoutTimer = null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
_handlePong() {
|
|
305
|
+
console.log("[WS] 收到 Pong");
|
|
306
|
+
this._clearHeartbeatTimeout();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// --- 重连机制 ---
|
|
310
|
+
_scheduleReconnect() {
|
|
311
|
+
if (this.forcedClose || this.isReconnecting || this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
312
|
+
if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
313
|
+
console.error("[WS] 已达到最大重连次数,停止重连");
|
|
314
|
+
this._emit("reconnect-failed");
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
this.isReconnecting = true;
|
|
320
|
+
const interval = Math.min(this.options.reconnectBaseInterval * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectInterval);
|
|
321
|
+
|
|
322
|
+
console.log(`[WS] ${interval / 1000}秒后将尝试第 ${this.reconnectAttempts + 1} 次重连...`);
|
|
323
|
+
this._emit("reconnect-attempt", { attempt: this.reconnectAttempts + 1, interval });
|
|
324
|
+
|
|
325
|
+
this.reconnectTimer = setTimeout(() => {
|
|
326
|
+
this.reconnectAttempts++;
|
|
327
|
+
this.connect();
|
|
328
|
+
}, interval);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
_clearReconnectTimer() {
|
|
332
|
+
if (this.reconnectTimer) {
|
|
333
|
+
clearTimeout(this.reconnectTimer);
|
|
334
|
+
this.reconnectTimer = null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// --- 消息队列 ---
|
|
339
|
+
_flushMessageQueue() {
|
|
340
|
+
if (this.messageQueue.length === 0) return;
|
|
341
|
+
console.log(`[WS] 发送队列中的 ${this.messageQueue.length} 条消息`);
|
|
342
|
+
const queue = [...this.messageQueue];
|
|
343
|
+
this.messageQueue = [];
|
|
344
|
+
queue.forEach((data) => this.send(data));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// --- 事件系统 ---
|
|
348
|
+
_emit(eventName, ...args) {
|
|
349
|
+
if (this.listeners.has(eventName)) {
|
|
350
|
+
this.listeners.get(eventName).forEach((callback) => callback(...args));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// --- 状态与监听器管理 ---
|
|
355
|
+
_updateReadyState(newState) {
|
|
356
|
+
this.readyState = newState;
|
|
357
|
+
this._emit("ready-state-change", newState);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
_setupEventListeners() {
|
|
361
|
+
document.addEventListener("visibilitychange", this._handleVisibilityChange);
|
|
362
|
+
window.addEventListener("online", this._handleOnline);
|
|
363
|
+
window.addEventListener("offline", this._handleOffline);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
_removeEventListeners() {
|
|
367
|
+
document.removeEventListener("visibilitychange", this._handleVisibilityChange);
|
|
368
|
+
window.removeEventListener("online", this._handleOnline);
|
|
369
|
+
window.removeEventListener("offline", this._handleOffline);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
_handleVisibilityChange() {
|
|
373
|
+
// 如果未启用心跳,不需要处理页面可见性变化
|
|
374
|
+
if (!this.options.getPingMessage) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (document.hidden) {
|
|
379
|
+
console.log("[WS] 页面隐藏,停止心跳");
|
|
380
|
+
this._stopHeartbeat();
|
|
381
|
+
} else {
|
|
382
|
+
console.log("[WS] 页面可见,检查连接状态");
|
|
383
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
384
|
+
this._startHeartbeat();
|
|
385
|
+
} else if (!this.forcedClose && !this.isReconnecting) {
|
|
386
|
+
this.connect();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
_handleOnline() {
|
|
392
|
+
console.log("[WS] 网络已恢复,尝试重连");
|
|
393
|
+
if (!this.forcedClose && this.readyState !== WebSocket.OPEN) {
|
|
394
|
+
this._clearReconnectTimer(); // 清除当前的重连计划
|
|
395
|
+
this.connect(); // 立即尝试连接
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
_handleOffline() {
|
|
400
|
+
console.log("[WS] 网络已断开");
|
|
401
|
+
this._clearReconnectTimer(); // 停止重连尝试
|
|
402
|
+
// ws.onclose 会被触发,从而启动重连逻辑,但我们已经停止了
|
|
403
|
+
// 所以这里可以手动触发一次 close 事件来通知应用层
|
|
404
|
+
if (this.ws) this.ws.onclose({ code: 1006, reason: "Network offline" });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
exports.WebSocketManager = WebSocketManager;
|
|
409
|
+
//# sourceMappingURL=webSocket.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webSocket.cjs","sources":["../src/source/webSocket.js"],"sourcesContent":["/**\n * @class WebSocketManager - 一个纯粹、强大的 WebSocket 连接管理引擎\n *\n * @features\n * - 智能断线重连 (指数退避算法)\n * - 网络状态监听\n * - 高度可定制的心跳保活机制 (通过注入函数实现)\n * - 页面可见性 API 集成\n * - 消息发送队列\n * - 清晰的生命周期管理\n * - 优雅的资源销毁\n * - 可配置的数据序列化/反序列化\n * - 纯粹的消息传递,不关心消息内容\n */\nexport class WebSocketManager {\n /**\n * @param {string} url - WebSocket 服务器的地址\n * @param {object} [options={}] - 配置选项\n * @param {number} [options.heartbeatInterval=30000] - 心跳间隔 (毫秒)\n * @param {number} [options.heartbeatTimeout=10000] - 心跳超时时间 (毫秒)\n * @param {number} [options.reconnectBaseInterval=1000] - 重连基础间隔 (毫秒)\n * @param {number} [options.maxReconnectInterval=30000] - 最大重连间隔 (毫秒)\n * @param {number} [options.maxReconnectAttempts=Infinity] - 最大重连次数\n * @param {boolean} [options.autoConnect=true] - 是否在实例化后自动连接\n * @param {boolean} [options.serializeData=false] - 发送数据时是否自动序列化为JSON字符串\n * @param {boolean} [options.deserializeData=false] - 接收数据时是否自动反序列化为JSON对象\n * @param {function|null} [options.getPingMessage=null] - 返回要发送的 ping 消息内容的函数。如果为 null,则不发送心跳。\n * @param {function|null} [options.isPongMessage=null] - 判断接收到的消息是否为 pong 的函数。接收 MessageEvent 对象作为参数,返回布尔值。\n * @param {object} [options.protocols] - WebSocket 协议\n */\n constructor(url, options = {}) {\n this.url = url;\n\n // 默认的心跳实现,用于向后兼容\n const defaultGetPingMessage = () => JSON.stringify({ type: \"ping\" });\n const defaultIsPongMessage = (event) => {\n try {\n const message = JSON.parse(event.data);\n return message.type === \"pong\";\n } catch (e) {\n return false;\n }\n };\n\n this.options = {\n heartbeatInterval: 30000,\n heartbeatTimeout: 10000,\n reconnectBaseInterval: 1000,\n maxReconnectInterval: 30000,\n maxReconnectAttempts: Infinity,\n autoConnect: true,\n serializeData: false,\n deserializeData: false,\n getPingMessage: defaultGetPingMessage, // 默认提供 ping 消息生成器\n isPongMessage: defaultIsPongMessage, // 默认提供 pong 消息判断器\n ...options\n };\n\n // WebSocket 实例\n this.ws = null;\n\n // 状态管理\n this.readyState = WebSocket.CLOSED;\n this.reconnectAttempts = 0;\n this.forcedClose = false;\n this.isReconnecting = false;\n\n // 定时器\n this.heartbeatTimer = null;\n this.heartbeatTimeoutTimer = null;\n this.reconnectTimer = null;\n\n // 消息队列\n this.messageQueue = [];\n\n // 事件监听器\n this.listeners = new Map();\n\n // 绑定方法上下文\n this._onOpen = this._onOpen.bind(this);\n this._onMessage = this._onMessage.bind(this);\n this._onClose = this._onClose.bind(this);\n this._onError = this._onError.bind(this);\n this._handleVisibilityChange = this._handleVisibilityChange.bind(this);\n this._handleOnline = this._handleOnline.bind(this);\n this._handleOffline = this._handleOffline.bind(this);\n\n this._setupEventListeners();\n\n if (this.options.autoConnect) {\n this.connect();\n }\n }\n\n // --- 公共 API ---\n\n /**\n * 连接到 WebSocket 服务器\n */\n connect() {\n if (this.ws && (this.ws.readyState === WebSocket.CONNECTING || this.ws.readyState === WebSocket.OPEN)) {\n return;\n }\n\n this.forcedClose = false;\n this._updateReadyState(WebSocket.CONNECTING);\n console.log(`[WS] 正在连接到 ${this.url}...`);\n this._emit(\"connecting\");\n\n try {\n this.ws = new WebSocket(this.url, this.options.protocols);\n this.ws.onopen = this._onOpen;\n this.ws.onmessage = this._onMessage;\n this.ws.onclose = this._onClose;\n this.ws.onerror = this._onError;\n } catch (error) {\n console.error(\"[WS] 连接失败:\", error);\n this._onError(error);\n }\n }\n\n /**\n * 发送数据\n * @param {string|object|ArrayBuffer|Blob} data - 要发送的数据\n */\n send(data) {\n if (this.readyState === WebSocket.OPEN) {\n let message;\n // 根据配置决定是否序列化数据\n if (this.options.serializeData) {\n message = JSON.stringify(data);\n } else {\n message = data;\n }\n this.ws.send(message);\n console.log(\"[WS] 消息已发送:\", message);\n } else {\n console.warn(\"[WS] 连接未打开,消息已加入队列:\", data);\n this.messageQueue.push(data);\n }\n }\n\n /**\n * 主动关闭连接\n * @param {number} [code=1000] - 关闭代码\n * @param {string} [reason=''] - 关闭原因\n */\n close(code = 1000, reason = \"Normal closure\") {\n this.forcedClose = true;\n this._stopHeartbeat();\n this._clearReconnectTimer();\n if (this.ws && this.ws.readyState === WebSocket.OPEN) {\n this.ws.close(code, reason);\n } else {\n this._updateReadyState(WebSocket.CLOSED);\n this._emit(\"close\", { code, reason, wasClean: true });\n }\n }\n\n /**\n * 彻底销毁实例,清理所有资源\n */\n destroy() {\n console.log(\"[WS] 正在销毁实例...\");\n this.close(1000, \"Instance destroyed\");\n this._removeEventListeners();\n this.messageQueue = [];\n this.listeners.clear();\n this.ws = null;\n }\n\n /**\n * 添加事件监听器\n * @param {string} eventName - 事件名 (e.g., 'open', 'message', 'close', 'error', 'reconnect')\n * @param {function} callback - 回调函数\n */\n on(eventName, callback) {\n if (!this.listeners.has(eventName)) {\n this.listeners.set(eventName, []);\n }\n this.listeners.get(eventName).push(callback);\n }\n\n /**\n * 移除事件监听器\n * @param {string} eventName - 事件名\n * @param {function} callback - 回调函数\n */\n off(eventName, callback) {\n if (this.listeners.has(eventName)) {\n const callbacks = this.listeners.get(eventName);\n const index = callbacks.indexOf(callback);\n if (index > -1) {\n callbacks.splice(index, 1);\n }\n }\n }\n\n // --- 内部方法 ---\n\n _onOpen(event) {\n console.log(\"[WS] 连接已建立\");\n this._updateReadyState(WebSocket.OPEN);\n this.reconnectAttempts = 0;\n this.isReconnecting = false;\n\n // 如果配置了 getPingMessage,则启动心跳\n if (this.options.getPingMessage) {\n this._startHeartbeat();\n }\n\n this._flushMessageQueue();\n this._emit(\"open\", event);\n }\n\n /**\n * 纯粹的消息处理方法:处理心跳,然后将数据交给使用者\n * @param {MessageEvent} event\n */\n _onMessage(event) {\n // --- 步骤 1: 使用注入的函数优先处理内部心跳机制 ---\n if (this.options.isPongMessage && this.options.isPongMessage(event)) {\n this._handlePong();\n return; // 是心跳消息,处理完毕,直接返回\n }\n\n // --- 步骤 2: 根据用户配置处理业务消息 ---\n if (this.options.deserializeData) {\n try {\n const parsedMessage = JSON.parse(event.data);\n this._emit(\"message\", parsedMessage, event);\n } catch (e) {\n this._emit(\"message\", event.data, event);\n }\n } else {\n this._emit(\"message\", event.data, event);\n }\n }\n\n _onClose(event) {\n console.log(\"[WS] 连接已关闭\", event);\n this._updateReadyState(WebSocket.CLOSED);\n this._stopHeartbeat();\n this._emit(\"close\", event);\n\n if (!this.forcedClose) {\n this._scheduleReconnect();\n }\n }\n\n _onError(event) {\n console.error(\"[WS] 连接发生错误:\", event);\n this._emit(\"error\", event);\n }\n\n // --- 心跳机制 ---\n _startHeartbeat() {\n // 如果没有配置 getPingMessage,则无法启动心跳\n if (!this.options.getPingMessage) {\n return;\n }\n\n this._stopHeartbeat();\n this.heartbeatTimer = setInterval(() => {\n if (this.ws && this.ws.readyState === WebSocket.OPEN) {\n try {\n // 调用注入的函数获取消息内容并发送\n const pingMessage = this.options.getPingMessage();\n this.ws.send(pingMessage);\n console.log(\"[WS] 发送 Ping:\", pingMessage);\n this._setHeartbeatTimeout();\n } catch (error) {\n console.error(\"[WS] 发送 Ping 消息失败:\", error);\n }\n }\n }, this.options.heartbeatInterval);\n }\n\n _stopHeartbeat() {\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer);\n this.heartbeatTimer = null;\n }\n this._clearHeartbeatTimeout();\n }\n\n _setHeartbeatTimeout() {\n this._clearHeartbeatTimeout();\n this.heartbeatTimeoutTimer = setTimeout(() => {\n console.error(\"[WS] 心跳超时,主动断开连接\");\n this.ws.close(1006, \"Heartbeat timeout\");\n }, this.options.heartbeatTimeout);\n }\n\n _clearHeartbeatTimeout() {\n if (this.heartbeatTimeoutTimer) {\n clearTimeout(this.heartbeatTimeoutTimer);\n this.heartbeatTimeoutTimer = null;\n }\n }\n\n _handlePong() {\n console.log(\"[WS] 收到 Pong\");\n this._clearHeartbeatTimeout();\n }\n\n // --- 重连机制 ---\n _scheduleReconnect() {\n if (this.forcedClose || this.isReconnecting || this.reconnectAttempts >= this.options.maxReconnectAttempts) {\n if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {\n console.error(\"[WS] 已达到最大重连次数,停止重连\");\n this._emit(\"reconnect-failed\");\n }\n return;\n }\n\n this.isReconnecting = true;\n const interval = Math.min(this.options.reconnectBaseInterval * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectInterval);\n\n console.log(`[WS] ${interval / 1000}秒后将尝试第 ${this.reconnectAttempts + 1} 次重连...`);\n this._emit(\"reconnect-attempt\", { attempt: this.reconnectAttempts + 1, interval });\n\n this.reconnectTimer = setTimeout(() => {\n this.reconnectAttempts++;\n this.connect();\n }, interval);\n }\n\n _clearReconnectTimer() {\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n }\n\n // --- 消息队列 ---\n _flushMessageQueue() {\n if (this.messageQueue.length === 0) return;\n console.log(`[WS] 发送队列中的 ${this.messageQueue.length} 条消息`);\n const queue = [...this.messageQueue];\n this.messageQueue = [];\n queue.forEach((data) => this.send(data));\n }\n\n // --- 事件系统 ---\n _emit(eventName, ...args) {\n if (this.listeners.has(eventName)) {\n this.listeners.get(eventName).forEach((callback) => callback(...args));\n }\n }\n\n // --- 状态与监听器管理 ---\n _updateReadyState(newState) {\n this.readyState = newState;\n this._emit(\"ready-state-change\", newState);\n }\n\n _setupEventListeners() {\n document.addEventListener(\"visibilitychange\", this._handleVisibilityChange);\n window.addEventListener(\"online\", this._handleOnline);\n window.addEventListener(\"offline\", this._handleOffline);\n }\n\n _removeEventListeners() {\n document.removeEventListener(\"visibilitychange\", this._handleVisibilityChange);\n window.removeEventListener(\"online\", this._handleOnline);\n window.removeEventListener(\"offline\", this._handleOffline);\n }\n\n _handleVisibilityChange() {\n // 如果未启用心跳,不需要处理页面可见性变化\n if (!this.options.getPingMessage) {\n return;\n }\n\n if (document.hidden) {\n console.log(\"[WS] 页面隐藏,停止心跳\");\n this._stopHeartbeat();\n } else {\n console.log(\"[WS] 页面可见,检查连接状态\");\n if (this.ws && this.ws.readyState === WebSocket.OPEN) {\n this._startHeartbeat();\n } else if (!this.forcedClose && !this.isReconnecting) {\n this.connect();\n }\n }\n }\n\n _handleOnline() {\n console.log(\"[WS] 网络已恢复,尝试重连\");\n if (!this.forcedClose && this.readyState !== WebSocket.OPEN) {\n this._clearReconnectTimer(); // 清除当前的重连计划\n this.connect(); // 立即尝试连接\n }\n }\n\n _handleOffline() {\n console.log(\"[WS] 网络已断开\");\n this._clearReconnectTimer(); // 停止重连尝试\n // ws.onclose 会被触发,从而启动重连逻辑,但我们已经停止了\n // 所以这里可以手动触发一次 close 事件来通知应用层\n if (this.ws) this.ws.onclose({ code: 1006, reason: \"Network offline\" });\n }\n}\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAM,gBAAgB,CAAC;AAC9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,EAAE,EAAE;AACnC,QAAQ,IAAI,CAAC,GAAG,GAAG,GAAG;;AAEtB;AACA,QAAQ,MAAM,qBAAqB,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAC5E,QAAQ,MAAM,oBAAoB,GAAG,CAAC,KAAK,KAAK;AAChD,YAAY,IAAI;AAChB,gBAAgB,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;AACtD,gBAAgB,OAAO,OAAO,CAAC,IAAI,KAAK,MAAM;AAC9C,YAAY,CAAC,CAAC,OAAO,CAAC,EAAE;AACxB,gBAAgB,OAAO,KAAK;AAC5B,YAAY;AACZ,QAAQ,CAAC;;AAET,QAAQ,IAAI,CAAC,OAAO,GAAG;AACvB,YAAY,iBAAiB,EAAE,KAA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