no message
This commit is contained in:
parent
a15732a789
commit
50fa3fe16e
@ -1,19 +1,20 @@
|
|||||||
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) {
|
||||||
try {
|
try {
|
||||||
socket = new WebSocket(socketurl);
|
socket = new WebSocket(socketurl);
|
||||||
socket.onopen = function() {
|
socket.onopen = function () {
|
||||||
console.log('WebSocket连接成功');
|
console.log('WebSocket连接成功');
|
||||||
var user = tool.data.get('user') || {};
|
var user = tool.data.get('user') || {};
|
||||||
var token = tool.cookie.get(config.SESSIONNAME);
|
var token = tool.cookie.get(config.SESSIONNAME);
|
||||||
@ -32,18 +33,21 @@ 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'
|
||||||
}));
|
}));
|
||||||
}, heartbeatInterval);
|
}, heartbeatInterval);
|
||||||
}
|
}
|
||||||
socket.onerror = function() {
|
socket.onerror = function () {
|
||||||
console.log('WebSocket连接发生错误');
|
console.log('WebSocket连接发生错误');
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
socket.onclose = function() {
|
socket.onclose = function () {
|
||||||
console.log('WebSocket连接关闭');
|
console.log('WebSocket连接关闭');
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
@ -58,7 +62,7 @@ export function createWebSocket() {
|
|||||||
|
|
||||||
function reconnect() {
|
function reconnect() {
|
||||||
if (maxReconnectAttempts > reconnectAttempts) {
|
if (maxReconnectAttempts > reconnectAttempts) {
|
||||||
setTimeout(function() {
|
setTimeout(function () {
|
||||||
createWebSocket();
|
createWebSocket();
|
||||||
console.log('WebSocket进行重连(' + reconnectAttempts);
|
console.log('WebSocket进行重连(' + reconnectAttempts);
|
||||||
reconnectAttempts++;
|
reconnectAttempts++;
|
||||||
@ -67,7 +71,9 @@ function reconnect() {
|
|||||||
console.log('WebSocket重连失败');
|
console.log('WebSocket重连失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createWebSocket();
|
createWebSocket();
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
state: socketState
|
state: socketState
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user