@wu529778790/open-im 1.1.2 → 1.1.3
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 +35 -5
- package/package.json +1 -1
package/dist/wework/client.js
CHANGED
|
@@ -110,12 +110,30 @@ export async function initWeWork(cfg, eventHandler, onStateChange) {
|
|
|
110
110
|
messageHandler = eventHandler;
|
|
111
111
|
stateChangeHandler = onStateChange ?? null;
|
|
112
112
|
log.info(`Initializing WeWork client (botId: ${config.botId})`);
|
|
113
|
-
|
|
113
|
+
// 首次连接支持重试:单独启用企微时偶发 TLS 连接失败,加飞书后因初始化顺序有“预热”效果则稳定
|
|
114
|
+
const maxAttempts = 3;
|
|
115
|
+
const retryDelayMs = 1500;
|
|
116
|
+
let lastErr = null;
|
|
117
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
118
|
+
try {
|
|
119
|
+
await connectWebSocket(true); // true = 初始连接,close 时不 scheduleReconnect
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
lastErr = err instanceof Error ? err : new Error(String(err));
|
|
124
|
+
if (attempt < maxAttempts) {
|
|
125
|
+
log.warn(`WeWork connection attempt ${attempt}/${maxAttempts} failed (${lastErr.message}), retrying in ${retryDelayMs}ms...`);
|
|
126
|
+
await new Promise((r) => setTimeout(r, retryDelayMs));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
throw lastErr ?? new Error('WeWork connection failed');
|
|
114
131
|
}
|
|
115
132
|
/**
|
|
116
133
|
* Connect to WeWork WebSocket server
|
|
134
|
+
* @param isInitialConnect - 初始连接时为 true,close 时不 scheduleReconnect(由 initWeWork 重试)
|
|
117
135
|
*/
|
|
118
|
-
async function connectWebSocket() {
|
|
136
|
+
async function connectWebSocket(isInitialConnect = false) {
|
|
119
137
|
if (connectionState === 'connecting') {
|
|
120
138
|
log.warn('WebSocket connection already in progress');
|
|
121
139
|
return;
|
|
@@ -123,8 +141,18 @@ async function connectWebSocket() {
|
|
|
123
141
|
if (!config) {
|
|
124
142
|
throw new Error('WeWork config not initialized');
|
|
125
143
|
}
|
|
144
|
+
// 重试前清理旧连接
|
|
145
|
+
if (ws) {
|
|
146
|
+
try {
|
|
147
|
+
ws.removeAllListeners();
|
|
148
|
+
ws.close();
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
/* ignore */
|
|
152
|
+
}
|
|
153
|
+
ws = null;
|
|
154
|
+
}
|
|
126
155
|
updateState('connecting');
|
|
127
|
-
// Store config values locally to avoid null issues in Promise callback
|
|
128
156
|
const websocketUrl = config.websocketUrl || DEFAULT_WS_URL;
|
|
129
157
|
return new Promise((resolve, reject) => {
|
|
130
158
|
try {
|
|
@@ -162,7 +190,9 @@ async function connectWebSocket() {
|
|
|
162
190
|
log.info('WeWork WebSocket closed');
|
|
163
191
|
stopHeartbeat();
|
|
164
192
|
updateState('disconnected');
|
|
165
|
-
|
|
193
|
+
if (!isInitialConnect) {
|
|
194
|
+
scheduleReconnect();
|
|
195
|
+
}
|
|
166
196
|
});
|
|
167
197
|
}
|
|
168
198
|
catch (err) {
|
|
@@ -331,7 +361,7 @@ function scheduleReconnect() {
|
|
|
331
361
|
reconnectAttempts++;
|
|
332
362
|
log.info(`Reconnecting... Attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS}`);
|
|
333
363
|
try {
|
|
334
|
-
await connectWebSocket();
|
|
364
|
+
await connectWebSocket(false); // 非初始连接,close 时继续 scheduleReconnect
|
|
335
365
|
}
|
|
336
366
|
catch (err) {
|
|
337
367
|
log.error('Reconnection failed:', err);
|