@qy_better_lib/hooks 0.2.6 → 0.2.8

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.
@@ -1,205 +0,0 @@
1
- import { is_object } from "@qy_better_lib/core";
2
- function use_web_socket(options) {
3
- const {
4
- server,
5
- receive,
6
- max_reconnect_attempts = 5,
7
- reconnect_interval = 5e3,
8
- heartbeat_interval = 3e4,
9
- heartbeat_message = "ping",
10
- on_open,
11
- on_error,
12
- on_close,
13
- auto_reconnect = true,
14
- auto_connect = true,
15
- max_message_queue_size = 100
16
- } = options;
17
- let reconnect_count = 0;
18
- let lock_reconnect = false;
19
- let ws = void 0;
20
- let reconnect_timer = null;
21
- let heartbeat_timer = null;
22
- let message_queue = [];
23
- let last_heartbeat_time = 0;
24
- const heartbeat_timeout = heartbeat_interval * 1.5;
25
- if (typeof window !== "undefined") {
26
- window.onbeforeunload = () => {
27
- close();
28
- };
29
- }
30
- function create() {
31
- try {
32
- console.log("正在连接WebSocket:", server);
33
- ws = new WebSocket(server);
34
- init_event_handlers();
35
- } catch (error) {
36
- console.error("WebSocket连接建立异常:", error);
37
- on_error?.(error);
38
- if (auto_reconnect) {
39
- reconnect();
40
- }
41
- }
42
- }
43
- function init_event_handlers() {
44
- if (!ws) return;
45
- ws.onopen = () => {
46
- console.log("WebSocket连接成功:", server);
47
- reconnect_count = 0;
48
- last_heartbeat_time = Date.now();
49
- start_heartbeat();
50
- flush_message_queue();
51
- on_open?.();
52
- };
53
- ws.onerror = (error) => {
54
- console.error("WebSocket连接错误:", error);
55
- on_error?.(error);
56
- };
57
- ws.onclose = (event) => {
58
- console.log("WebSocket连接关闭:", (/* @__PURE__ */ new Date()).toLocaleTimeString(), "原因:", event.code, event.reason);
59
- stop_heartbeat();
60
- on_close?.();
61
- if (auto_reconnect && !event.wasClean) {
62
- reconnect();
63
- }
64
- };
65
- ws.onmessage = (event) => {
66
- try {
67
- last_heartbeat_time = Date.now();
68
- receive(event);
69
- } catch (error) {
70
- console.error("WebSocket消息处理错误:", error);
71
- console.log("原始消息:", event.data);
72
- }
73
- };
74
- }
75
- function reconnect() {
76
- if (reconnect_count >= max_reconnect_attempts) {
77
- console.error("WebSocket重连失败次数过多,停止重连");
78
- close();
79
- return;
80
- }
81
- if (lock_reconnect) return;
82
- lock_reconnect = true;
83
- console.log(`WebSocket尝试重连 ${reconnect_count + 1}/${max_reconnect_attempts}...`);
84
- reconnect_timer = setTimeout(() => {
85
- create();
86
- reconnect_count++;
87
- lock_reconnect = false;
88
- clear_reconnect_timer();
89
- }, reconnect_interval);
90
- }
91
- function clear_reconnect_timer() {
92
- if (reconnect_timer) {
93
- clearTimeout(reconnect_timer);
94
- reconnect_timer = null;
95
- }
96
- }
97
- function start_heartbeat() {
98
- stop_heartbeat();
99
- heartbeat_timer = setInterval(() => {
100
- send_heartbeat();
101
- check_heartbeat_timeout();
102
- }, heartbeat_interval);
103
- }
104
- function stop_heartbeat() {
105
- if (heartbeat_timer) {
106
- clearInterval(heartbeat_timer);
107
- heartbeat_timer = null;
108
- }
109
- }
110
- function send_heartbeat() {
111
- if (ws?.readyState === WebSocket.OPEN) {
112
- try {
113
- const heartbeat_msg = is_object(heartbeat_message) ? JSON.stringify(heartbeat_message) : heartbeat_message;
114
- ws.send(heartbeat_msg);
115
- } catch (error) {
116
- console.error("发送心跳消息失败:", error);
117
- }
118
- }
119
- }
120
- function check_heartbeat_timeout() {
121
- const now = Date.now();
122
- if (now - last_heartbeat_time > heartbeat_timeout) {
123
- console.error("WebSocket心跳超时,尝试重连");
124
- reconnect();
125
- }
126
- }
127
- function send_message(msg) {
128
- if (ws?.readyState === WebSocket.OPEN) {
129
- try {
130
- const message = is_object(msg) ? JSON.stringify(msg) : msg;
131
- ws.send(message);
132
- return true;
133
- } catch (error) {
134
- console.error("发送消息失败:", error);
135
- add_to_message_queue(msg);
136
- return false;
137
- }
138
- } else {
139
- console.warn("WebSocket未连接,消息已加入队列");
140
- add_to_message_queue(msg);
141
- return false;
142
- }
143
- }
144
- function add_to_message_queue(msg) {
145
- if (message_queue.length >= max_message_queue_size) {
146
- const removed_msg = message_queue.shift();
147
- console.warn("消息队列已满,移除最旧的消息:", removed_msg);
148
- }
149
- message_queue.push(msg);
150
- console.log(`消息已加入队列,当前队列大小: ${message_queue.length}/${max_message_queue_size}`);
151
- }
152
- function flush_message_queue() {
153
- if (ws?.readyState === WebSocket.OPEN && message_queue.length > 0) {
154
- console.log(`发送队列中的 ${message_queue.length} 条消息`);
155
- while (message_queue.length > 0) {
156
- const msg = message_queue.shift();
157
- send_message(msg);
158
- }
159
- }
160
- }
161
- function close() {
162
- stop_heartbeat();
163
- clear_reconnect_timer();
164
- if (ws) {
165
- try {
166
- ws.close();
167
- } catch (error) {
168
- console.error("关闭WebSocket失败:", error);
169
- }
170
- ws = void 0;
171
- }
172
- message_queue = [];
173
- }
174
- function get_status() {
175
- if (!ws) return "CLOSED";
176
- switch (ws.readyState) {
177
- case WebSocket.CONNECTING:
178
- return "CONNECTING";
179
- case WebSocket.OPEN:
180
- return "OPEN";
181
- case WebSocket.CLOSING:
182
- return "CLOSING";
183
- case WebSocket.CLOSED:
184
- return "CLOSED";
185
- default:
186
- return "UNKNOWN";
187
- }
188
- }
189
- if (auto_connect) {
190
- create();
191
- }
192
- return {
193
- ws,
194
- create,
195
- close,
196
- send_message,
197
- get_status,
198
- reconnect,
199
- start_heartbeat,
200
- stop_heartbeat
201
- };
202
- }
203
- export {
204
- use_web_socket
205
- };
File without changes