From c46abeecf544c08e4a85f882775a48684473274e Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Fri, 14 Mar 2025 11:35:55 +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 | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/frontend/src/utils/webSocketClient.ts b/frontend/src/utils/webSocketClient.ts index d78013a..4cf872b 100644 --- a/frontend/src/utils/webSocketClient.ts +++ b/frontend/src/utils/webSocketClient.ts @@ -20,6 +20,7 @@ export default class SocketService { // 重新连接尝试的次数 connectRetryCount = 0; work:any; + workerBlobUrl:any; lastActivityTime= 0; // 上次活动时间戳 lastResponseHeartTime = Date.now();//最后一次收到心跳回复时间 @@ -107,9 +108,9 @@ export default class SocketService { startHeartbeat() { this.lastResponseHeartTime = Date.now(); const _this = this - const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},9000)})()'])); - this.workerBlobUrl = url; // 存储临时的Blob URL - this.work = new Worker(url); + _this.workerBlobUrl = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},9000)})()'])); + + this.work = new Worker(_this.workerBlobUrl); this.work.onmessage = function(e){ //判断多久没收到心跳响应 @@ -135,13 +136,14 @@ export default class SocketService { } clearHeartbeat() { - if (this.work) { - this.work.terminate(); - this.work = null; + const _this = this + if (_this.work) { + _this.work.terminate(); + _this.work = null; } - if (this.workerBlobUrl) { - window.URL.revokeObjectURL(this.workerBlobUrl); // 释放临时的Blob URL - this.workerBlobUrl = null; + if (_this.workerBlobUrl) { + window.URL.revokeObjectURL(_this.workerBlobUrl); // 释放临时的Blob URL + _this.workerBlobUrl = null; } }