Browse Source

ws reconnect

master
tangmingyou 4 years ago
parent
commit
4d71ea6335
  1. 4
      config/dev.js
  2. 13
      src/api/websocket.js

4
config/dev.js

@ -10,8 +10,8 @@ module.exports = {
h5: { h5: {
devServer: { devServer: {
//host: '0.0.0.0', //host: '0.0.0.0',
// host: '192.168.110.53', host: '192.168.110.53',
host: '192.168.3.104', // host: '192.168.3.104',
port: 10086, port: 10086,
// 设置代理来解决 H5 请求的跨域问题 // 设置代理来解决 H5 请求的跨域问题
proxy: { proxy: {

13
src/api/websocket.js

@ -34,6 +34,7 @@ const websocket = {
this.opSuccess = opSuccess; this.opSuccess = opSuccess;
this.opPathMap = opPathMap || {}; this.opPathMap = opPathMap || {};
this.nameOpMap = nameOpMap || {}; this.nameOpMap = nameOpMap || {};
this.wsAddr = wsAddr;
// window.proto = proto; // window.proto = proto;
const ws = new WebSocket(`ws://${wsAddr}/api/conn/ws`) const ws = new WebSocket(`ws://${wsAddr}/api/conn/ws`)
@ -46,6 +47,7 @@ const websocket = {
ws.onopen = (e) => { ws.onopen = (e) => {
// 关闭定时重连 // 关闭定时重连
clearInterval(this.reconnectInterval); clearInterval(this.reconnectInterval);
this.reconnectInterval = -1;
this.status = 1; this.status = 1;
// 连接后立即认证连接 // 连接后立即认证连接
@ -80,18 +82,19 @@ const websocket = {
this.status = 3; this.status = 3;
console.log('ws error:', a, b, c) console.log('ws error:', a, b, c)
this.reconnectInterval = setInterval(() => { // this.reconnectInterval = setInterval(() => {
this.reconnect(); // this.reconnect();
}, Math.random() * 3 + 3); // }, Math.random() * 3 + 3);
} }
ws.onclose = e => { ws.onclose = e => {
this.status = 3; this.status = 3;
console.log('ws close', e) console.log('ws close', e)
if (this.reconnectInterval < 0) {
this.reconnectInterval = setInterval(() => { this.reconnectInterval = setInterval(() => {
this.reconnect(); this.reconnect();
}, Math.random() * 3 + 3); }, Math.random() * 3 + 4);
}
} }
ws.onmessage = (e) => { ws.onmessage = (e) => {

Loading…
Cancel
Save