signalr 取消,长时间会有问题,打开浏览器会白屏
对接erp,接口 线边任务完成根据detailid,分入库 出库报工 todo 成品的报工(定时器回传 or 入库/拣货的时候回传)
This commit is contained in:
1 parent
00119151c9
commit
c3ec557c4d
19 files changed
+403
-362
No files matched your search
@@ -1,285 +1 @@
|
||||
<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;
|
||||
|
||||
// 服务异常检测
|
||||
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({
|
||||
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 || "您的账号已在其他设备登录,请重新登录", {
|
||||
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);
|
||||
onConnectionRestored();
|
||||
startHeartbeat();
|
||||
});
|
||||
|
||||
// 监听心跳响应
|
||||
loginConnection.on("HeartbeatResponse", function(serverTime) {
|
||||
//console.log("心跳响应:", serverTime);
|
||||
});
|
||||
|
||||
// 连接关闭事件
|
||||
loginConnection.onclose(function(error) {
|
||||
console.log("SignalR 连接已关闭", error);
|
||||
stopHeartbeat();
|
||||
|
||||
if (!isForceLogout) {
|
||||
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) {
|
||||
console.error("重新注册失败:", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 开始连接
|
||||
startConnection();
|
||||
}
|
||||
|
||||
// 启动连接
|
||||
function startConnection() {
|
||||
if (isForceLogout) return;
|
||||
|
||||
loginConnection.start()
|
||||
.then(function() {
|
||||
console.log("SignalR 连接已建立");
|
||||
onConnectionRestored();
|
||||
// 注册当前用户
|
||||
if (userId) {
|
||||
loginConnection.invoke("Register", userId).catch(function(err) {
|
||||
console.error("注册失败:", err);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error("SignalR 连接失败:", err);
|
||||
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(
|
||||
'<div id="reconnecting-status" style="position:fixed;top:0;left:0;right:0;background:#ff9800;color:#fff;text-align:center;padding:8px;z-index:99999;font-size:14px;">' +
|
||||
'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i> ' +
|
||||
'正在连接服务器... (尝试 ' + reconnectAttempts + '/' + maxReconnectAttempts + ')' +
|
||||
'</div>'
|
||||
);
|
||||
} else {
|
||||
$('#reconnecting-status').html(
|
||||
'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i> ' +
|
||||
'正在连接服务器... (尝试 ' + reconnectAttempts + '/' + maxReconnectAttempts + ')'
|
||||
).show();
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏重连中状态
|
||||
function hideReconnectingStatus() {
|
||||
$('#reconnecting-status').hide();
|
||||
}
|
||||
|
||||
// 显示服务不可用弹窗
|
||||
function showServerDownAlert() {
|
||||
if (isServerDown) return;
|
||||
isServerDown = true;
|
||||
hideReconnectingStatus();
|
||||
|
||||
// 显示服务不可用状态栏
|
||||
if ($('#server-down-status').length === 0) {
|
||||
$('body').append(
|
||||
'<div id="server-down-status" style="position:fixed;top:0;left:0;right:0;background:#f44336;color:#fff;text-align:center;padding:8px;z-index:99999;font-size:14px;">' +
|
||||
'<i class="layui-icon layui-icon-close-fill"></i> ' +
|
||||
'服务器连接已断开,请检查网络或联系管理员 ' +
|
||||
'<button onclick="manualReconnect()" style="margin-left:10px;padding:2px 10px;cursor:pointer;background:#fff;color:#f44336;border:none;border-radius:3px;">重新连接</button>' +
|
||||
'</div>'
|
||||
);
|
||||
} else {
|
||||
$('#server-down-status').show();
|
||||
}
|
||||
|
||||
// 弹窗提示
|
||||
if (!serverDownAlertIndex) {
|
||||
serverDownAlertIndex = layer.confirm('服务器连接已断开,可能是网络问题或服务器维护中。<br/>是否重新连接?', {
|
||||
icon: 2,
|
||||
title: "连接异常",
|
||||
btn: ['重新连接', '稍后再试'],
|
||||
closeBtn: 0
|
||||
}, function(index) {
|
||||
layer.close(index);
|
||||
serverDownAlertIndex = null;
|
||||
manualReconnect();
|
||||
}, function(index) {
|
||||
layer.close(index);
|
||||
serverDownAlertIndex = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏服务不可用弹窗
|
||||
function hideServerDownAlert() {
|
||||
$('#server-down-status').hide();
|
||||
if (serverDownAlertIndex) {
|
||||
layer.close(serverDownAlertIndex);
|
||||
serverDownAlertIndex = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 手动重连
|
||||
function manualReconnect() {
|
||||
reconnectAttempts = 0;
|
||||
isServerDown = false;
|
||||
hideServerDownAlert();
|
||||
showReconnectingStatus();
|
||||
startConnection();
|
||||
}
|
||||
|
||||
// 停止重连
|
||||
function stopReconnect() {
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 开始心跳
|
||||
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() {
|
||||
stopReconnect();
|
||||
stopHeartbeat();
|
||||
if (loginConnection) {
|
||||
loginConnection.stop();
|
||||
}
|
||||
});
|
||||
|
||||
// 页面从隐藏状态变为可见时,检查连接状态
|
||||
$(document).on('visibilitychange', function() {
|
||||
if (document.visibilityState === 'visible' && !isForceLogout) {
|
||||
if (loginConnection && loginConnection.state !== signalR.HubConnectionState.Connected) {
|
||||
if (!isServerDown) {
|
||||
manualReconnect();
|
||||
}
|
||||
} else if (loginConnection && userId) {
|
||||
// 页面可见时发送一次心跳
|
||||
loginConnection.invoke("Heartbeat", userId).catch(function(err) {
|
||||
console.error("心跳发送失败:", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user