diff --git a/HHWMS.Web/Views/Home/Index.cshtml b/HHWMS.Web/Views/Home/Index.cshtml index 686cee4..620607b 100644 --- a/HHWMS.Web/Views/Home/Index.cshtml +++ b/HHWMS.Web/Views/Home/Index.cshtml @@ -6,18 +6,34 @@ var heartbeatTimer = null; var userId = '@ViewData["ITCode"]'; // 从后端获取当前用户ID var isForceLogout = false; + + // 服务异常检测 + var reconnectAttempts = 0; // 重连尝试次数 + var maxReconnectAttempts = 5; // 最大重连次数 + var isServerDown = false; // 服务是否宕机 + var serverDownAlertIndex = null; // 服务宕机弹窗索引 + var reconnectTimer = null; // 重连定时器 // 初始化 SignalR 连接 function initLoginHub() { loginConnection = new signalR.HubConnectionBuilder() .withUrl("/loginHub") - .withAutomaticReconnect([0, 2000, 5000, 10000, 30000]) // 自动重连策略 + .withAutomaticReconnect({ + nextRetryDelayInMilliseconds: function(retryContext) { + // 自定义重连策略 + if (retryContext.previousRetryCount < maxReconnectAttempts) { + return Math.min(1000 * Math.pow(2, retryContext.previousRetryCount), 30000); + } + return null; // 停止自动重连 + } + }) .build(); // 监听强制登出事件 loginConnection.on("ForceLogout", function(message) { isForceLogout = true; stopHeartbeat(); + stopReconnect(); // 使用layer弹窗提示 layer.alert(message || "您的账号已在其他设备登录,请重新登录", { @@ -36,6 +52,7 @@ // 监听注册成功 loginConnection.on("RegisterSuccess", function(message) { console.log("SignalR 登录连接成功:", message); + onConnectionRestored(); startHeartbeat(); }); @@ -50,16 +67,21 @@ stopHeartbeat(); if (!isForceLogout) { - // 非强制登出,尝试重连 - setTimeout(function() { - startConnection(); - }, 5000); + onConnectionLost(); } }); + // 正在重连事件 + loginConnection.onreconnecting(function(error) { + console.log("SignalR 正在重连...", error); + reconnectAttempts++; + showReconnectingStatus(); + }); + // 重连成功事件 loginConnection.onreconnected(function(connectionId) { console.log("SignalR 重连成功:", connectionId); + onConnectionRestored(); // 重新注册 if (userId) { loginConnection.invoke("Register", userId).catch(function(err) { @@ -79,6 +101,7 @@ loginConnection.start() .then(function() { console.log("SignalR 连接已建立"); + onConnectionRestored(); // 注册当前用户 if (userId) { loginConnection.invoke("Register", userId).catch(function(err) { @@ -88,13 +111,126 @@ }) .catch(function(err) { console.error("SignalR 连接失败:", err); - // 连接失败,5秒后重试 - setTimeout(function() { - startConnection(); - }, 5000); + reconnectAttempts++; + + if (reconnectAttempts >= maxReconnectAttempts) { + // 达到最大重连次数,显示服务不可用 + showServerDownAlert(); + } else { + // 继续重连 + showReconnectingStatus(); + var delay = Math.min(1000 * Math.pow(2, reconnectAttempts), 30000); + reconnectTimer = setTimeout(function() { + startConnection(); + }, delay); + } }); } + // 连接丢失处理 + function onConnectionLost() { + reconnectAttempts = 0; + showReconnectingStatus(); + + // 手动启动重连 + reconnectTimer = setTimeout(function() { + startConnection(); + }, 2000); + } + + // 连接恢复处理 + function onConnectionRestored() { + reconnectAttempts = 0; + isServerDown = false; + hideReconnectingStatus(); + hideServerDownAlert(); + } + + // 显示重连中状态 + function showReconnectingStatus() { + if ($('#reconnecting-status').length === 0) { + $('body').append( + '