2024-12-20 16:32:03 +08:00
|
|
|
|
import {ElMessage} from "element-plus";
|
|
|
|
|
|
|
2025-01-17 15:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
export default class SocketService {
|
|
|
|
|
|
static instance = null;
|
|
|
|
|
|
static get Instance() {
|
|
|
|
|
|
if (!this.instance) {
|
|
|
|
|
|
this.instance = new SocketService();
|
|
|
|
|
|
}
|
|
|
|
|
|
return this.instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 和服务端连接的socket对象
|
|
|
|
|
|
ws = null;
|
|
|
|
|
|
// 存储回调函数
|
|
|
|
|
|
callBackMapping = {};
|
|
|
|
|
|
// 标识是否连接成功
|
|
|
|
|
|
connected = false;
|
|
|
|
|
|
// 记录重试的次数
|
|
|
|
|
|
sendRetryCount = 0;
|
|
|
|
|
|
// 重新连接尝试的次数
|
|
|
|
|
|
connectRetryCount = 0;
|
2025-01-17 15:06:44 +08:00
|
|
|
|
work:any;
|
2025-01-16 15:57:29 +08:00
|
|
|
|
heartbeatTimer;
|
|
|
|
|
|
heartbeatInterval = 6000; // 心跳间隔,单位毫秒
|
|
|
|
|
|
lastActivityTime= 0; // 上次活动时间戳
|
|
|
|
|
|
reconnectDelay= 5000; // 重新连接延迟,单位毫秒
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
// 定义连接服务器的方法
|
2025-01-17 09:14:19 +08:00
|
|
|
|
connect() {
|
2024-12-20 16:32:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 连接服务器
|
|
|
|
|
|
if (!window.WebSocket) {
|
|
|
|
|
|
return console.log('您的浏览器不支持WebSocket');
|
|
|
|
|
|
}
|
2025-01-17 09:14:19 +08:00
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
// let token = $.cookie('123');
|
|
|
|
|
|
// let token = '4E6EF539AAF119D82AC4C2BC84FBA21F';
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-17 15:06:44 +08:00
|
|
|
|
const url = 'ws://192.168.1.127:7777/hello?name=cdf'
|
2024-12-20 16:32:03 +08:00
|
|
|
|
this.ws = new WebSocket(url);
|
|
|
|
|
|
// 连接成功的事件
|
|
|
|
|
|
this.ws.onopen = () => {
|
|
|
|
|
|
ElMessage.success("webSocket连接服务端成功了");
|
|
|
|
|
|
console.log('连接服务端成功了');
|
|
|
|
|
|
this.connected = true;
|
|
|
|
|
|
// 重置重新连接的次数
|
|
|
|
|
|
this.connectRetryCount = 0;
|
2025-01-16 15:57:29 +08:00
|
|
|
|
this.updateLastActivityTime();
|
|
|
|
|
|
this.startHeartbeat();
|
2024-12-20 16:32:03 +08:00
|
|
|
|
};
|
|
|
|
|
|
// 1.连接服务端失败
|
|
|
|
|
|
// 2.当连接成功之后, 服务器关闭的情况
|
|
|
|
|
|
this.ws.onclose = () => {
|
|
|
|
|
|
console.log('连接服务端失败');
|
|
|
|
|
|
this.connected = false;
|
|
|
|
|
|
this.connectRetryCount++;
|
2025-01-16 15:57:29 +08:00
|
|
|
|
this.clearHeartbeat();
|
2024-12-20 16:32:03 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
// this.connect();
|
|
|
|
|
|
}, 500 * this.connectRetryCount);
|
2025-01-16 15:57:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
};
|
2025-01-17 09:14:19 +08:00
|
|
|
|
|
|
|
|
|
|
this.ws.onerror = () => {
|
|
|
|
|
|
ElMessage.error("webSocket连接异常!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
// 得到服务端发送过来的数据
|
|
|
|
|
|
this.ws.onmessage = (event) => {
|
|
|
|
|
|
let message: { [key: string]: any };
|
2025-01-16 15:57:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
try {
|
2025-01-08 15:15:49 +08:00
|
|
|
|
console.log('Received message:',event.data)
|
2024-12-20 16:32:03 +08:00
|
|
|
|
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 {
|
2025-01-16 15:57:29 +08:00
|
|
|
|
console.log("抛弃====>")
|
|
|
|
|
|
console.log(event.data)
|
|
|
|
|
|
/* 丢弃或继续写你的逻辑 */
|
2024-12-20 16:32:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-01-16 15:57:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startHeartbeat() {
|
2025-01-17 15:06:44 +08:00
|
|
|
|
let _this = this
|
|
|
|
|
|
var url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},3000)})()']));
|
|
|
|
|
|
this.work = new Worker(url)
|
|
|
|
|
|
this.work.onmessage = function(e){
|
|
|
|
|
|
if ((Date.now() - _this.lastActivityTime) > _this.heartbeatInterval) {
|
2025-01-16 15:57:29 +08:00
|
|
|
|
// 如果超过心跳间隔没有活动,发送心跳消息
|
2025-01-17 15:06:44 +08:00
|
|
|
|
_this.sendHeartbeat();
|
2025-01-16 15:57:29 +08:00
|
|
|
|
}
|
2025-01-17 15:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// this.heartbeatTimer = setInterval(() => {
|
|
|
|
|
|
// if ((Date.now() - this.lastActivityTime) > this.heartbeatInterval) {
|
|
|
|
|
|
// // 如果超过心跳间隔没有活动,发送心跳消息
|
|
|
|
|
|
// this.sendHeartbeat();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }, this.heartbeatInterval/2); // 每半个心跳间隔检查一次
|
2025-01-16 15:57:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
sendHeartbeat() {
|
|
|
|
|
|
console.log("进入心跳消息发送。。。。。。。。。。。。。")
|
|
|
|
|
|
this.ws.send('alive');
|
|
|
|
|
|
this.updateLastActivityTime(); // 发送心跳后更新活动时间
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateLastActivityTime() {
|
|
|
|
|
|
this.lastActivityTime = Date.now();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clearHeartbeat() {
|
2025-01-17 15:06:44 +08:00
|
|
|
|
this.work.terminate();
|
2025-01-16 15:57:29 +08:00
|
|
|
|
if (this.heartbeatTimer) {
|
2025-01-17 15:06:44 +08:00
|
|
|
|
|
2025-01-16 15:57:29 +08:00
|
|
|
|
clearInterval(this.heartbeatTimer);
|
|
|
|
|
|
this.heartbeatTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-20 16:32:03 +08:00
|
|
|
|
// 回调函数的注册
|
|
|
|
|
|
registerCallBack(socketType, callBack) {
|
|
|
|
|
|
this.callBackMapping[socketType] = callBack;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 取消某一个回调函数
|
|
|
|
|
|
unRegisterCallBack(socketType) {
|
|
|
|
|
|
this.callBackMapping[socketType] = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 发送数据的方法
|
|
|
|
|
|
send(data) {
|
|
|
|
|
|
// 判断此时此刻有没有连接成功
|
|
|
|
|
|
if (this.connected) {
|
|
|
|
|
|
this.sendRetryCount = 0;
|
|
|
|
|
|
try {
|
|
|
|
|
|
this.ws.send(JSON.stringify(data));
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
this.ws.send(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.sendRetryCount++;
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
this.send(data);
|
|
|
|
|
|
}, this.sendRetryCount * 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 断开方法
|
|
|
|
|
|
closeWs() {
|
|
|
|
|
|
if (this.connected) {
|
|
|
|
|
|
this.ws.close()
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('WS关闭中..');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|