Ver código fonte

优化websocket失败重连逻辑

sa03 6 anos atrás
pai
commit
84722d663d
2 arquivos alterados com 26 adições e 16 exclusões
  1. 1 1
      assets/scripts/Game.js
  2. 25 15
      assets/scripts/net/Ws.js

+ 1 - 1
assets/scripts/Game.js

@@ -177,7 +177,7 @@ cc.Class({
             autoConnect: true,          // 自动连接
             reconnection: true,         // 断开自动重连
             reconnectionDelay: 5000,    // 重连间隔时间,单位秒
-            reconnectionAttempts: 10
+            reconnectionAttempts: 5
         })
 
         let seq = 1;

+ 25 - 15
assets/scripts/net/Ws.js

@@ -201,9 +201,8 @@ function WsManager(url, opts) {
     this.reconnectionAttempts(opts.reconnectionAttempts || Infinity) //重连最大尝试次数
     this.timeout(null == opts.timeout ? 20000 : opts.timeout)
     this.logStyle = 'color:blue; font-size:16px;font-weight:bold;'
-    this.keepAliveInterval = 20000
-    this.keepAliveTimeout = null
-    this.keepAliveMsg = '0'
+    this.keepAliveInterval = 15000 //心跳包发送间隔
+    this.keepAliveTimeout = null //心跳包计时器
 
     this.autoConnect = opts.autoConnect !== false //是否自动连接
     if(this.autoConnect) {
@@ -230,28 +229,37 @@ WsManager.prototype.reconnect = function() {
         return;
     } else {
         if(this._reconnectTimes < this._reconnectionAttempts) {
+            console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
+
             if(this.socket) {
                 this.socket.close()
             }
+            
             this._reconnectTimes += 1
-    
-            console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
             this.readyState = 'reconnecting'
+
             setTimeout(() => {
                 this.socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
             }, this._reconnectionDelay);
         } else {
+            console.log(`%c [达到最大连续重连失败次数,已重置,30秒后重试]`, this.logStyle);
+
             if(this.socket) {
                 this.socket.close()
             }
-            console.log(`%c [达到最大重连失败次数,Socket关闭]`, this.logStyle);
+
+            this._reconnectTimes = 1
+            this.readyState = 'reconnecting'
+
+            setTimeout(() => {
+                this.socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
+            }, 30000);
         }
     }
 
 }
 
 WsManager.prototype.keepAlive = function() {
-    let alivemsg = this.keepAliveMsg
     this.keepAliveTimeout = setInterval(() => {
         if(this.readyState == 'open') {
             let payload = {
@@ -285,15 +293,16 @@ WsManager.prototype.openWxConnect = function() {
     })
 
     _socket.onClose((res) => {
+        console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
+
         this.readyState = 'closed'
         
         //只要关闭就重连(暂时性处理)
-        // if(this._reconnection) {
-        //     this.reconnect()
-        // }
+        if(this._reconnection) {
+            this.reconnect()
+        }
 
         this.emit('close', res)
-        console.log(`%c [Socket连接关闭: ${res}]`, this.logStyle)
     })
 
     _socket.onMessage((res) => {
@@ -334,18 +343,19 @@ WsManager.prototype.openH5Connect = function() {
     }
 
     _socket.onclose = (event) => {
+        console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
+
         this.readyState = 'closed';
         let code = event.code
         let reason = event.reason
         let wasClean = event.wasClean
 
         //只要关闭就重连(暂时性处理)
-        // if(this._reconnection) {
-        //     this.reconnect()
-        // }
+        if(this._reconnection) {
+            this.reconnect()
+        }
 
         this.emit('close', event)
-        console.log(`%c [Socket连接关闭: ${reason}]`, this.logStyle)
     }
 
     _socket.onmessage = (event) => {