no message

This commit is contained in:
小陌 2023-09-21 19:07:10 +08:00
parent a15732a789
commit 50fa3fe16e

View File

@ -1,13 +1,14 @@
import config from '@/config'; import config from '@/config';
import { import { ref } from 'vue';
ref
} from 'vue';
import tool from '@/utils/tool'; import tool from '@/utils/tool';
let socket; let socket;
let reconnectAttempts = 1; let reconnectAttempts = 1;
let intervalId; // 用于保存心跳请求的定时器id
const maxReconnectAttempts = 5; const maxReconnectAttempts = 5;
const heartbeatInterval = 50000; const heartbeatInterval = 50000;
const socketState = ref(socket); const socketState = ref(socket);
export function createWebSocket() { export function createWebSocket() {
let socketurl = config.WEBSOCKET || null; let socketurl = config.WEBSOCKET || null;
if (socketurl) { if (socketurl) {
@ -32,8 +33,11 @@ export function createWebSocket() {
} }
reconnectAttempts = 0; // reset reconnect attempts reconnectAttempts = 0; // reset reconnect attempts
// 清除之前的定时器
clearInterval(intervalId);
// 定时发送心跳请求 // 定时发送心跳请求
setInterval(function() { intervalId = setInterval(function () {
socket.send(JSON.stringify({ socket.send(JSON.stringify({
type: 'ping' type: 'ping'
})); }));
@ -67,7 +71,9 @@ function reconnect() {
console.log('WebSocket重连失败'); console.log('WebSocket重连失败');
} }
} }
createWebSocket(); createWebSocket();
export default { export default {
state: socketState state: socketState
} }