Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -172,8 +172,9 @@ class ResponseHandler {
|
|
172 |
|
173 |
// WebSocket工具类
|
174 |
class WebSocketUtils {
|
175 |
-
static
|
176 |
static TIMEOUT = 5 * 60 * 1000; // 5分钟超时时间
|
|
|
177 |
|
178 |
// 生成WebSocket密钥
|
179 |
static generateWebSocketKey() {
|
@@ -182,11 +183,11 @@ class WebSocketUtils {
|
|
182 |
|
183 |
// 创建WebSocket客户端
|
184 |
static async createWebSocketClient(requestPayload) {
|
185 |
-
|
186 |
-
|
|
|
187 |
}
|
188 |
|
189 |
-
this.isProcessing = true;
|
190 |
let timeoutId;
|
191 |
let ws;
|
192 |
|
@@ -200,11 +201,17 @@ class WebSocketUtils {
|
|
200 |
}
|
201 |
});
|
202 |
|
|
|
|
|
|
|
|
|
203 |
// 设置超时处理
|
204 |
timeoutId = setTimeout(() => {
|
205 |
if (ws.readyState === WebSocket.OPEN) {
|
206 |
ws.close();
|
207 |
}
|
|
|
|
|
208 |
reject(new Error('WebSocket连接超时(5分钟)'));
|
209 |
}, this.TIMEOUT);
|
210 |
|
@@ -227,6 +234,7 @@ class WebSocketUtils {
|
|
227 |
ws.on('message', async (data) => {
|
228 |
const message = data.toString();
|
229 |
const parsedMessage = JSON.parse(message);
|
|
|
230 |
|
231 |
switch (parsedMessage.type) {
|
232 |
case 'connection_ack':
|
@@ -254,7 +262,8 @@ class WebSocketUtils {
|
|
254 |
ws.on('error', (err) => {
|
255 |
console.error('WebSocket错误:', err);
|
256 |
clearTimeout(timeoutId);
|
257 |
-
this.
|
|
|
258 |
if (ws.readyState === WebSocket.OPEN) {
|
259 |
ws.close();
|
260 |
}
|
@@ -264,7 +273,8 @@ class WebSocketUtils {
|
|
264 |
ws.on('close', (code, reason) => {
|
265 |
console.log('请求完毕,关闭连接');
|
266 |
clearTimeout(timeoutId);
|
267 |
-
this.
|
|
|
268 |
if (!isComplete) {
|
269 |
reject(new Error('WebSocket closed unexpectedly'));
|
270 |
}
|
@@ -272,9 +282,12 @@ class WebSocketUtils {
|
|
272 |
});
|
273 |
} catch (error) {
|
274 |
clearTimeout(timeoutId);
|
275 |
-
|
276 |
-
|
277 |
-
|
|
|
|
|
|
|
278 |
}
|
279 |
throw error;
|
280 |
}
|
@@ -330,6 +343,11 @@ class WebSocketUtils {
|
|
330 |
}
|
331 |
return null;
|
332 |
}
|
|
|
|
|
|
|
|
|
|
|
333 |
}
|
334 |
|
335 |
// 创建Express应用
|
|
|
172 |
|
173 |
// WebSocket工具类
|
174 |
class WebSocketUtils {
|
175 |
+
static activeConnections = new Set(); // 跟踪活跃连接
|
176 |
static TIMEOUT = 5 * 60 * 1000; // 5分钟超时时间
|
177 |
+
static MAX_CONNECTIONS = 10; // 最大并发连接数
|
178 |
|
179 |
// 生成WebSocket密钥
|
180 |
static generateWebSocketKey() {
|
|
|
183 |
|
184 |
// 创建WebSocket客户端
|
185 |
static async createWebSocketClient(requestPayload) {
|
186 |
+
// 检查当前连接数是否达到上限
|
187 |
+
if (this.activeConnections.size >= this.MAX_CONNECTIONS) {
|
188 |
+
throw new Error(`当前连接数已达到上限 (${this.MAX_CONNECTIONS}),请稍后重试喵!`);
|
189 |
}
|
190 |
|
|
|
191 |
let timeoutId;
|
192 |
let ws;
|
193 |
|
|
|
201 |
}
|
202 |
});
|
203 |
|
204 |
+
// 添加到活跃连接集合
|
205 |
+
this.activeConnections.add(ws);
|
206 |
+
console.log(`当前活跃连接数: ${this.activeConnections.size}/${this.MAX_CONNECTIONS}`);
|
207 |
+
|
208 |
// 设置超时处理
|
209 |
timeoutId = setTimeout(() => {
|
210 |
if (ws.readyState === WebSocket.OPEN) {
|
211 |
ws.close();
|
212 |
}
|
213 |
+
this.activeConnections.delete(ws);
|
214 |
+
console.log(`连接超时,当前活跃连接数: ${this.activeConnections.size}/${this.MAX_CONNECTIONS}`);
|
215 |
reject(new Error('WebSocket连接超时(5分钟)'));
|
216 |
}, this.TIMEOUT);
|
217 |
|
|
|
234 |
ws.on('message', async (data) => {
|
235 |
const message = data.toString();
|
236 |
const parsedMessage = JSON.parse(message);
|
237 |
+
console.log(JSON.stringify(parsedMessage,null,2));
|
238 |
|
239 |
switch (parsedMessage.type) {
|
240 |
case 'connection_ack':
|
|
|
262 |
ws.on('error', (err) => {
|
263 |
console.error('WebSocket错误:', err);
|
264 |
clearTimeout(timeoutId);
|
265 |
+
this.activeConnections.delete(ws);
|
266 |
+
console.log(`连接错误,当前活跃连接数: ${this.activeConnections.size}/${this.MAX_CONNECTIONS}`);
|
267 |
if (ws.readyState === WebSocket.OPEN) {
|
268 |
ws.close();
|
269 |
}
|
|
|
273 |
ws.on('close', (code, reason) => {
|
274 |
console.log('请求完毕,关闭连接');
|
275 |
clearTimeout(timeoutId);
|
276 |
+
this.activeConnections.delete(ws);
|
277 |
+
console.log(`连接关闭,当前活跃连接数: ${this.activeConnections.size}/${this.MAX_CONNECTIONS}`);
|
278 |
if (!isComplete) {
|
279 |
reject(new Error('WebSocket closed unexpectedly'));
|
280 |
}
|
|
|
282 |
});
|
283 |
} catch (error) {
|
284 |
clearTimeout(timeoutId);
|
285 |
+
if (ws) {
|
286 |
+
this.activeConnections.delete(ws);
|
287 |
+
console.log(`发生错误,当前活跃连接数: ${this.activeConnections.size}/${this.MAX_CONNECTIONS}`);
|
288 |
+
if (ws.readyState === WebSocket.OPEN) {
|
289 |
+
ws.close();
|
290 |
+
}
|
291 |
}
|
292 |
throw error;
|
293 |
}
|
|
|
343 |
}
|
344 |
return null;
|
345 |
}
|
346 |
+
|
347 |
+
// 获取当前活跃连接数
|
348 |
+
static getActiveConnectionsCount() {
|
349 |
+
return this.activeConnections.size;
|
350 |
+
}
|
351 |
}
|
352 |
|
353 |
// 创建Express应用
|