Kaynağa Gözat

优化websocket重连逻辑

sa03 6 yıl önce
ebeveyn
işleme
cd0193b2b4
1 değiştirilmiş dosya ile 23 ekleme ve 6 silme
  1. 23 6
      assets/scripts/net/Ws.js

+ 23 - 6
assets/scripts/net/Ws.js

@@ -229,8 +229,6 @@ 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()
             }
@@ -238,6 +236,8 @@ WsManager.prototype.reconnect = function() {
             this._reconnectTimes += 1
             this.readyState = 'reconnecting'
 
+            console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
+
             setTimeout(() => {
                 this.socket = CC_WECHATGAME ? this.openWxConnect() : this.openH5Connect()
             }, this._reconnectionDelay);
@@ -288,12 +288,17 @@ WsManager.prototype.openWxConnect = function() {
         this.emit('open', res)
         console.log(`%c [Socket连接成功: ${this.url.split("&token")[0]}]`, this.logStyle);
 
+        // 连接成功,重置重连次数
+        this._reconnectTimes = 1
+
         // 每隔一段时间发一个心跳包保持连接状态
         this.keepAlive()
     })
 
     _socket.onClose((res) => {
-        console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
+        console.log(`%c [Socket连接被关闭: ${res}]`, this.logStyle)
+
+        clearInterval(this.keepAliveTimeout)
 
         this.readyState = 'closed'
         
@@ -313,6 +318,10 @@ WsManager.prototype.openWxConnect = function() {
     })
 
     _socket.onError((res) => {
+        console.log(`%c [Socket错误: ${res.errMsg}]`, this.logStyle);
+
+        clearInterval(this.keepAliveTimeout)
+
         if(this._reconnection) {
             if(this.readyState != 'reconnecting') {
                 this.readyState = 'error';
@@ -323,7 +332,6 @@ WsManager.prototype.openWxConnect = function() {
         }
 
         this.emit('error', res.errMsg)
-        console.log(`%c [Socket错误: ${res.errMsg}]`, this.logStyle);
     })
 
     return _socket
@@ -338,24 +346,30 @@ WsManager.prototype.openH5Connect = function() {
         this.emit('open', event)
         console.log(`%c [Socket连接成功: ${this.url.split("&token")[0]}]`, this.logStyle);
 
+        // 连接成功,重置重连次数
+        this._reconnectTimes = 1
+        
         // 每隔一段时间发一个心跳包保持连接状态
         this.keepAlive()
     }
 
     _socket.onclose = (event) => {
-        console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
+        clearInterval(this.keepAliveTimeout)
 
         this.readyState = 'closed';
         let code = event.code
         let reason = event.reason
         let wasClean = event.wasClean
 
+        console.log(`%c [Socket连接被关闭: ${reason}]`, this.logStyle)
+
         //只要关闭就重连(暂时性处理)
         if(this._reconnection) {
             this.reconnect()
         }
 
         this.emit('close', event)
+
     }
 
     _socket.onmessage = (event) => {
@@ -366,6 +380,10 @@ WsManager.prototype.openH5Connect = function() {
     }
 
     _socket.onerror = (event) => {
+        console.log(`%c [Socket错误: ${JSON.stringify(event)}]`, this.logStyle);
+
+        clearInterval(this.keepAliveTimeout)
+
         if(this._reconnection) {
             if(this.readyState != 'reconnecting') {
                 this.readyState = 'error';
@@ -376,7 +394,6 @@ WsManager.prototype.openH5Connect = function() {
         }
 
         this.emit(event)
-        console.log(`%c [Socket错误: ${JSON.stringify(event)}]`, this.logStyle);
     }
     return _socket
 }