@wu529778790/open-im 1.4.0 → 1.4.1
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/dist/wework/client.js +18 -5
- package/package.json +1 -1
package/dist/wework/client.js
CHANGED
|
@@ -20,6 +20,8 @@ let connectionState = 'disconnected';
|
|
|
20
20
|
let reconnectTimer = null;
|
|
21
21
|
let heartbeatTimer = null;
|
|
22
22
|
let reconnectAttempts = 0;
|
|
23
|
+
let shouldReconnect = false;
|
|
24
|
+
let isStopping = false;
|
|
23
25
|
// Event handlers
|
|
24
26
|
let messageHandler = null;
|
|
25
27
|
let stateChangeHandler = null;
|
|
@@ -109,6 +111,8 @@ export async function initWeWork(cfg, eventHandler, onStateChange) {
|
|
|
109
111
|
};
|
|
110
112
|
messageHandler = eventHandler;
|
|
111
113
|
stateChangeHandler = onStateChange ?? null;
|
|
114
|
+
isStopping = false;
|
|
115
|
+
shouldReconnect = false;
|
|
112
116
|
log.info(`Initializing WeWork client (botId: ${config.botId})`);
|
|
113
117
|
// 首次连接支持重试:单独启用企微时偶发 TLS 连接失败,加飞书后因初始化顺序有“预热”效果则稳定
|
|
114
118
|
const maxAttempts = 3;
|
|
@@ -116,7 +120,7 @@ export async function initWeWork(cfg, eventHandler, onStateChange) {
|
|
|
116
120
|
let lastErr = null;
|
|
117
121
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
118
122
|
try {
|
|
119
|
-
await connectWebSocket(
|
|
123
|
+
await connectWebSocket();
|
|
120
124
|
return;
|
|
121
125
|
}
|
|
122
126
|
catch (err) {
|
|
@@ -131,9 +135,8 @@ export async function initWeWork(cfg, eventHandler, onStateChange) {
|
|
|
131
135
|
}
|
|
132
136
|
/**
|
|
133
137
|
* Connect to WeWork WebSocket server
|
|
134
|
-
* @param isInitialConnect - 初始连接时为 true,close 时不 scheduleReconnect(由 initWeWork 重试)
|
|
135
138
|
*/
|
|
136
|
-
async function connectWebSocket(
|
|
139
|
+
async function connectWebSocket() {
|
|
137
140
|
if (connectionState === 'connecting') {
|
|
138
141
|
log.warn('WebSocket connection already in progress');
|
|
139
142
|
return;
|
|
@@ -165,6 +168,7 @@ async function connectWebSocket(isInitialConnect = false) {
|
|
|
165
168
|
// 发送认证订阅消息,并等待服务端确认(否则 aibot_send_msg 会报 846609 not subscribed)
|
|
166
169
|
try {
|
|
167
170
|
await sendSubscribeAndWaitAck(resolve, reject);
|
|
171
|
+
shouldReconnect = true;
|
|
168
172
|
log.info('WeWork authentication successful');
|
|
169
173
|
}
|
|
170
174
|
catch (err) {
|
|
@@ -190,7 +194,7 @@ async function connectWebSocket(isInitialConnect = false) {
|
|
|
190
194
|
log.info('WeWork WebSocket closed');
|
|
191
195
|
stopHeartbeat();
|
|
192
196
|
updateState('disconnected');
|
|
193
|
-
if (!
|
|
197
|
+
if (!isStopping && shouldReconnect) {
|
|
194
198
|
scheduleReconnect();
|
|
195
199
|
}
|
|
196
200
|
});
|
|
@@ -355,16 +359,23 @@ function stopHeartbeat() {
|
|
|
355
359
|
* Schedule reconnection attempt
|
|
356
360
|
*/
|
|
357
361
|
function scheduleReconnect() {
|
|
362
|
+
if (isStopping || !shouldReconnect) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
358
365
|
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
|
359
366
|
log.error('Max reconnect attempts reached');
|
|
360
367
|
return;
|
|
361
368
|
}
|
|
362
369
|
const interval = 5000; // 5秒后重连
|
|
370
|
+
if (reconnectTimer) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
363
373
|
reconnectTimer = setTimeout(async () => {
|
|
374
|
+
reconnectTimer = null;
|
|
364
375
|
reconnectAttempts++;
|
|
365
376
|
log.info(`Reconnecting... Attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS}`);
|
|
366
377
|
try {
|
|
367
|
-
await connectWebSocket(
|
|
378
|
+
await connectWebSocket();
|
|
368
379
|
}
|
|
369
380
|
catch (err) {
|
|
370
381
|
log.error('Reconnection failed:', err);
|
|
@@ -375,6 +386,8 @@ function scheduleReconnect() {
|
|
|
375
386
|
* Stop WeWork client
|
|
376
387
|
*/
|
|
377
388
|
export function stopWeWork() {
|
|
389
|
+
isStopping = true;
|
|
390
|
+
shouldReconnect = false;
|
|
378
391
|
stopHeartbeat();
|
|
379
392
|
if (reconnectTimer) {
|
|
380
393
|
clearTimeout(reconnectTimer);
|