fix(aibox): 缩短 WebSocket 心跳间隔并补全直连页面心跳
前置代理(Nginx/CDN)的 proxy_read_timeout 约 20s,原 25s 心跳 无法在超时前到达,导致连接被代理切断。aibox.js 心跳从 25s 缩短至 10s;frontend.html 直连页面此前完全无心跳,新增 10s 应用层心跳。
This commit is contained in:
1 parent
bcb1d5d557
commit
7ff73b7d07
2 files changed
+12
-2
No files matched your search
@@ -1355,6 +1355,8 @@
|
||||
let reconnectAttempts = 0;
|
||||
const MAX_RECONNECT_ATTEMPTS = 5;
|
||||
let manualDisconnect = false;
|
||||
let heartbeatTimer = null;
|
||||
const HEARTBEAT_INTERVAL = 10000;
|
||||
|
||||
function connectWebSocket() {
|
||||
const wsUrl = document.getElementById('ws-url').value.trim();
|
||||
@@ -1379,6 +1381,13 @@
|
||||
reconnectAttempts = 0;
|
||||
updateConnectionStatus(true);
|
||||
showSystemMessage(`✅ WebSocket 连接成功!服务器: ${wsUrl}`);
|
||||
// 启动心跳保活,防止前置代理空闲超时断连
|
||||
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
||||
heartbeatTimer = setInterval(() => {
|
||||
if (webSocket.readyState === WebSocket.OPEN) {
|
||||
webSocket.send(JSON.stringify({ type: 'ping' }));
|
||||
}
|
||||
}, HEARTBEAT_INTERVAL);
|
||||
};
|
||||
|
||||
webSocket.onmessage = (event) => {
|
||||
@@ -1399,6 +1408,7 @@
|
||||
|
||||
webSocket.onclose = (event) => {
|
||||
console.log('WebSocket 断开连接:', event.code, event.reason);
|
||||
if (heartbeatTimer) { clearInterval(heartbeatTimer); heartbeatTimer = null; }
|
||||
updateConnectionStatus(false);
|
||||
|
||||
if (!manualDisconnect && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||
|
||||
@@ -44,8 +44,8 @@ let ws = null;
|
||||
let manualClose = false; /* 手动断开则抑制自动重连 */
|
||||
let reconnectDelay = RECONNECT_MIN;
|
||||
let reconnectTimer = null;
|
||||
let heartbeatTimer = null; /* 心跳定时器,25s 发送 ping 保持 WS 连接 */
|
||||
const HEARTBEAT_INTERVAL = 25000; /* 心跳间隔(< gateway 30s 空闲超时) */
|
||||
let heartbeatTimer = null; /* 心跳定时器,10s 发送 ping 保持 WS 连接 */
|
||||
const HEARTBEAT_INTERVAL = 10000; /* 心跳间隔:需低于前置代理(Nginx/CDN)的 proxy_read_timeout */
|
||||
|
||||
function startHeartbeat() {
|
||||
stopHeartbeat();
|
||||
|
||||
Reference in new issue
Block a user