From c06bd9fa2440ce11b6e10312576ac165c41a1f02 Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Wed, 19 Feb 2025 11:00:09 +0800 Subject: [PATCH] =?UTF-8?q?websocket=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/utils/webSocketClient.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frontend/src/utils/webSocketClient.ts b/frontend/src/utils/webSocketClient.ts index 1de6574..a277408 100644 --- a/frontend/src/utils/webSocketClient.ts +++ b/frontend/src/utils/webSocketClient.ts @@ -53,7 +53,7 @@ export default class SocketService { // 1.连接服务端失败 // 2.当连接成功之后, 服务器关闭的情况 this.ws.onclose = () => { - console.log('连接服务端失败'); + console.log('连接webSocket服务端关闭'); this.connected = false; this.connectRetryCount++; this.clearHeartbeat(); @@ -76,7 +76,7 @@ export default class SocketService { if(event.data == 'over') { //心跳消息处理 this.lastResponseHeartTime = Date.now(); - + this.updateLastActivityTime(); // 收到心跳响应时更新活动时间 }else { let message: { [key: string]: any }; try { @@ -106,7 +106,7 @@ export default class SocketService { startHeartbeat() { const _this = this - const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},10000)})()'])); + const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},9000)})()'])); this.work = new Worker(url) this.work.onmessage = function(e){ //判断多久没收到心跳响应 @@ -114,6 +114,7 @@ export default class SocketService { if(_this.lastActivityTime - _this.lastResponseHeartTime > 30000){ //说明已经三轮心跳没收到回复了,关闭检测,提示用户。 ElMessage.error("业务主体模块发生未知异常,请尝试重新启动!"); + _this.clearHeartbeat(); return; } _this.sendHeartbeat(); @@ -172,6 +173,6 @@ export default class SocketService { if (this.connected) { this.ws.close() } - console.log('WS关闭中..'); + console.log('执行WS关闭命令..'); } }