websocket代码提交

This commit is contained in:
2025-02-18 14:59:13 +08:00
parent c66d181b98
commit 7d3e86a5fc

View File

@@ -1,9 +1,6 @@
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
export default class SocketService { export default class SocketService {
static instance = null; static instance = null;
static get Instance() { static get Instance() {
@@ -24,8 +21,9 @@ export default class SocketService {
connectRetryCount = 0; connectRetryCount = 0;
work:any; work:any;
heartbeatTimer; heartbeatTimer;
heartbeatInterval = 6000; // 心跳间隔,单位毫秒
lastActivityTime= 0; // 上次活动时间戳 lastActivityTime= 0; // 上次活动时间戳
lastResponseHeartTime = Date.now();//最后一次收到心跳回复时间
reconnectDelay= 5000; // 重新连接延迟,单位毫秒 reconnectDelay= 5000; // 重新连接延迟,单位毫秒
// 定义连接服务器的方法 // 定义连接服务器的方法
@@ -59,9 +57,9 @@ export default class SocketService {
this.connected = false; this.connected = false;
this.connectRetryCount++; this.connectRetryCount++;
this.clearHeartbeat(); this.clearHeartbeat();
setTimeout(() => { /* setTimeout(() => {
// this.connect(); this.connect();
}, 500 * this.connectRetryCount); }, 500 * this.connectRetryCount);*/
}; };
@@ -72,26 +70,33 @@ export default class SocketService {
}; };
// 得到服务端发送过来的数据 // 得到服务端发送过来的数据
this.ws.onmessage = (event) => { 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 { /* 通过接受服务端发送的type字段来回调函数 */
console.log('Received message:',event.data) if (message?.type && this.callBackMapping[message.type]) {
message = JSON.parse(event.data); this.callBackMapping[message.type](message);
} catch (e) { } else {
return console.error("消息解析失败", event.data, e);
}
//console.log(message, "从服务端获取到了数据");
/* 通过接受服务端发送的type字段来回调函数 */
if (message?.type && this.callBackMapping[message.type]) {
this.callBackMapping[message.type](message);
} else {
console.log("抛弃====>") console.log("抛弃====>")
console.log(event.data) console.log(event.data)
/* 丢弃或继续写你的逻辑 */ /* 丢弃或继续写你的逻辑 */
}
} }
}; };
} }
@@ -100,25 +105,23 @@ export default class SocketService {
startHeartbeat() { startHeartbeat() {
let _this = this const _this = this
var url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},3000)})()'])); const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},10000)})()']));
this.work = new Worker(url) this.work = new Worker(url)
this.work.onmessage = function(e){ 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() { sendHeartbeat() {
console.log("进入心跳消息发送。。。。。。。。。。。。。") console.log(new Date()+"进入心跳消息发送。。。。。。。。。。。。。")
this.ws.send('alive'); this.ws.send('alive');
this.updateLastActivityTime(); // 发送心跳后更新活动时间 this.updateLastActivityTime(); // 发送心跳后更新活动时间
} }
@@ -131,7 +134,6 @@ export default class SocketService {
clearHeartbeat() { clearHeartbeat() {
this.work.terminate(); this.work.terminate();
if (this.heartbeatTimer) { if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer); clearInterval(this.heartbeatTimer);
this.heartbeatTimer = null; this.heartbeatTimer = null;
} }