From 4d71ea63351eb88122a500fb545433dff602db68 Mon Sep 17 00:00:00 2001 From: tangmingyou <234767776@qq.com> Date: Wed, 22 Feb 2023 18:01:03 +0800 Subject: [PATCH] ws reconnect --- config/dev.js | 4 ++-- src/api/websocket.js | 17 ++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config/dev.js b/config/dev.js index 6cb3900..fbc99a3 100644 --- a/config/dev.js +++ b/config/dev.js @@ -10,8 +10,8 @@ module.exports = { h5: { devServer: { //host: '0.0.0.0', - // host: '192.168.110.53', - host: '192.168.3.104', + host: '192.168.110.53', + // host: '192.168.3.104', port: 10086, // 设置代理来解决 H5 请求的跨域问题 proxy: { diff --git a/src/api/websocket.js b/src/api/websocket.js index eca3743..33889af 100644 --- a/src/api/websocket.js +++ b/src/api/websocket.js @@ -34,6 +34,7 @@ const websocket = { this.opSuccess = opSuccess; this.opPathMap = opPathMap || {}; this.nameOpMap = nameOpMap || {}; + this.wsAddr = wsAddr; // window.proto = proto; const ws = new WebSocket(`ws://${wsAddr}/api/conn/ws`) @@ -46,6 +47,7 @@ const websocket = { ws.onopen = (e) => { // 关闭定时重连 clearInterval(this.reconnectInterval); + this.reconnectInterval = -1; this.status = 1; // 连接后立即认证连接 @@ -80,18 +82,19 @@ const websocket = { this.status = 3; console.log('ws error:', a, b, c) - this.reconnectInterval = setInterval(() => { - this.reconnect(); - }, Math.random() * 3 + 3); + // this.reconnectInterval = setInterval(() => { + // this.reconnect(); + // }, Math.random() * 3 + 3); } ws.onclose = e => { this.status = 3; console.log('ws close', e) - - this.reconnectInterval = setInterval(() => { - this.reconnect(); - }, Math.random() * 3 + 3); + if (this.reconnectInterval < 0) { + this.reconnectInterval = setInterval(() => { + this.reconnect(); + }, Math.random() * 3 + 4); + } } ws.onmessage = (e) => {