diff --git a/frontend/src/utils/webSocketClient.ts b/frontend/src/utils/webSocketClient.ts index 293b086..1de6574 100644 --- a/frontend/src/utils/webSocketClient.ts +++ b/frontend/src/utils/webSocketClient.ts @@ -1,9 +1,6 @@ import {ElMessage} from "element-plus"; - - - export default class SocketService { static instance = null; static get Instance() { @@ -24,8 +21,9 @@ export default class SocketService { connectRetryCount = 0; work:any; heartbeatTimer; - heartbeatInterval = 6000; // 心跳间隔,单位毫秒 lastActivityTime= 0; // 上次活动时间戳 + lastResponseHeartTime = Date.now();//最后一次收到心跳回复时间 + reconnectDelay= 5000; // 重新连接延迟,单位毫秒 // 定义连接服务器的方法 @@ -59,9 +57,9 @@ export default class SocketService { this.connected = false; this.connectRetryCount++; this.clearHeartbeat(); - setTimeout(() => { - // this.connect(); - }, 500 * this.connectRetryCount); + /* setTimeout(() => { + this.connect(); + }, 500 * this.connectRetryCount);*/ }; @@ -72,26 +70,33 @@ export default class SocketService { }; + // 得到服务端发送过来的数据 this.ws.onmessage = (event) => { - let message: { [key: string]: any }; + if(event.data == 'over') { + //心跳消息处理 + this.lastResponseHeartTime = Date.now(); + }else { + let message: { [key: string]: any }; + try { + console.log('Received message:',event.data) + message = JSON.parse(event.data); + } catch (e) { + return console.error("消息解析失败", event.data, e); + } - try { - console.log('Received message:',event.data) - message = JSON.parse(event.data); - } catch (e) { - return console.error("消息解析失败", event.data, e); - } - //console.log(message, "从服务端获取到了数据"); - /* 通过接受服务端发送的type字段来回调函数 */ - if (message?.type && this.callBackMapping[message.type]) { - this.callBackMapping[message.type](message); - } else { + /* 通过接受服务端发送的type字段来回调函数 */ + if (message?.type && this.callBackMapping[message.type]) { + this.callBackMapping[message.type](message); + } else { console.log("抛弃====>") console.log(event.data) /* 丢弃或继续写你的逻辑 */ + } } + + }; } @@ -100,25 +105,23 @@ export default class SocketService { startHeartbeat() { - let _this = this - var url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},3000)})()'])); + const _this = this + const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},10000)})()'])); this.work = new Worker(url) this.work.onmessage = function(e){ - if ((Date.now() - _this.lastActivityTime) > _this.heartbeatInterval) { - // 如果超过心跳间隔没有活动,发送心跳消息 - _this.sendHeartbeat(); + //判断多久没收到心跳响应 + + if(_this.lastActivityTime - _this.lastResponseHeartTime > 30000){ + //说明已经三轮心跳没收到回复了,关闭检测,提示用户。 + ElMessage.error("业务主体模块发生未知异常,请尝试重新启动!"); + return; } - + _this.sendHeartbeat(); } - // this.heartbeatTimer = setInterval(() => { - // if ((Date.now() - this.lastActivityTime) > this.heartbeatInterval) { - // // 如果超过心跳间隔没有活动,发送心跳消息 - // this.sendHeartbeat(); - // } - // }, this.heartbeatInterval/2); // 每半个心跳间隔检查一次 + } sendHeartbeat() { - console.log("进入心跳消息发送。。。。。。。。。。。。。") + console.log(new Date()+"进入心跳消息发送。。。。。。。。。。。。。") this.ws.send('alive'); this.updateLastActivityTime(); // 发送心跳后更新活动时间 } @@ -131,7 +134,6 @@ export default class SocketService { clearHeartbeat() { this.work.terminate(); if (this.heartbeatTimer) { - clearInterval(this.heartbeatTimer); this.heartbeatTimer = null; }