- 在Program.cs中注册LoginHub - 在HomeController中传递用户ID以支持SignalR连接 - 在Index.cshtml中实现SignalR连接和心跳机制,处理强制登出和注册成功事件 - 移除旧的登录状态检查逻辑,优化页面加载时的连接管理
145 lines
4.8 KiB
Plaintext
145 lines
4.8 KiB
Plaintext
<script>layui.use('index')</script>
|
|
<script src="/front/signalr.min.js"></script>
|
|
<script>
|
|
// SignalR 登录状态管理
|
|
var loginConnection = null;
|
|
var heartbeatTimer = null;
|
|
var userId = '@ViewData["ITCode"]'; // 从后端获取当前用户ID
|
|
var isForceLogout = false;
|
|
|
|
// 初始化 SignalR 连接
|
|
function initLoginHub() {
|
|
loginConnection = new signalR.HubConnectionBuilder()
|
|
.withUrl("/loginHub")
|
|
.withAutomaticReconnect([0, 2000, 5000, 10000, 30000]) // 自动重连策略
|
|
.build();
|
|
|
|
// 监听强制登出事件
|
|
loginConnection.on("ForceLogout", function(message) {
|
|
isForceLogout = true;
|
|
stopHeartbeat();
|
|
|
|
// 使用layer弹窗提示
|
|
layer.alert(message || "您的账号已在其他设备登录,请重新登录", {
|
|
icon: 0,
|
|
title: "登录提示",
|
|
closeBtn: 0,
|
|
btn: ['确定'],
|
|
yes: function(index) {
|
|
layer.close(index);
|
|
// 重定向到登录页
|
|
window.location.href = '/Login/Login';
|
|
}
|
|
});
|
|
});
|
|
|
|
// 监听注册成功
|
|
loginConnection.on("RegisterSuccess", function(message) {
|
|
console.log("SignalR 登录连接成功:", message);
|
|
startHeartbeat();
|
|
});
|
|
|
|
// 监听心跳响应
|
|
loginConnection.on("HeartbeatResponse", function(serverTime) {
|
|
//console.log("心跳响应:", serverTime);
|
|
});
|
|
|
|
// 连接关闭事件
|
|
loginConnection.onclose(function(error) {
|
|
console.log("SignalR 连接已关闭", error);
|
|
stopHeartbeat();
|
|
|
|
if (!isForceLogout) {
|
|
// 非强制登出,尝试重连
|
|
setTimeout(function() {
|
|
startConnection();
|
|
}, 5000);
|
|
}
|
|
});
|
|
|
|
// 重连成功事件
|
|
loginConnection.onreconnected(function(connectionId) {
|
|
console.log("SignalR 重连成功:", connectionId);
|
|
// 重新注册
|
|
if (userId) {
|
|
loginConnection.invoke("Register", userId).catch(function(err) {
|
|
console.error("重新注册失败:", err);
|
|
});
|
|
}
|
|
});
|
|
|
|
// 开始连接
|
|
startConnection();
|
|
}
|
|
|
|
// 启动连接
|
|
function startConnection() {
|
|
if (isForceLogout) return;
|
|
|
|
loginConnection.start()
|
|
.then(function() {
|
|
console.log("SignalR 连接已建立");
|
|
// 注册当前用户
|
|
if (userId) {
|
|
loginConnection.invoke("Register", userId).catch(function(err) {
|
|
console.error("注册失败:", err);
|
|
});
|
|
}
|
|
})
|
|
.catch(function(err) {
|
|
console.error("SignalR 连接失败:", err);
|
|
// 连接失败,5秒后重试
|
|
setTimeout(function() {
|
|
startConnection();
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
// 开始心跳
|
|
function startHeartbeat() {
|
|
stopHeartbeat();
|
|
heartbeatTimer = setInterval(function() {
|
|
if (loginConnection && loginConnection.state === signalR.HubConnectionState.Connected) {
|
|
loginConnection.invoke("Heartbeat", userId).catch(function(err) {
|
|
console.error("心跳发送失败:", err);
|
|
});
|
|
}
|
|
}, 30000); // 每30秒发送一次心跳
|
|
}
|
|
|
|
// 停止心跳
|
|
function stopHeartbeat() {
|
|
if (heartbeatTimer) {
|
|
clearInterval(heartbeatTimer);
|
|
heartbeatTimer = null;
|
|
}
|
|
}
|
|
|
|
// 页面加载完成后初始化
|
|
$(document).ready(function() {
|
|
if (userId) {
|
|
initLoginHub();
|
|
}
|
|
|
|
// 页面关闭时断开连接
|
|
$(window).on('beforeunload', function() {
|
|
if (loginConnection) {
|
|
loginConnection.stop();
|
|
}
|
|
});
|
|
|
|
// 页面从隐藏状态变为可见时,检查连接状态
|
|
$(document).on('visibilitychange', function() {
|
|
if (document.visibilityState === 'visible' && !isForceLogout) {
|
|
if (loginConnection && loginConnection.state !== signalR.HubConnectionState.Connected) {
|
|
startConnection();
|
|
} else if (loginConnection && userId) {
|
|
// 页面可见时发送一次心跳
|
|
loginConnection.invoke("Heartbeat", userId).catch(function(err) {
|
|
console.error("心跳发送失败:", err);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
});
|
|
</script> |