11
0

2 Commits 7e3b2e8d6d ... e641ce9671

Autor SHA1 Nachricht Datum
  tang biao e641ce9671 Merge branch 'dev' of http://svn.ouj.com:3000/DWG/taptapstar into dev vor 5 Jahren
  tang biao c6766db7ab 添加头条socket vor 5 Jahren
1 geänderte Dateien mit 77 neuen und 10 gelöschten Zeilen
  1. 77 10
      assets/scripts/net/Ws.js

+ 77 - 10
assets/scripts/net/Ws.js

@@ -215,10 +215,12 @@ WsManager.prototype.connect = function(fn) {
     }
 
     this.readyState = 'opening';
-    if (CC_WECHATGAME) {
-        this.socket = this.openWxConnect();
+    if (window.tt != undefined) {
+        this.socket = this.openTTConnect();
     } else if (CC_QQPLAY) {
         this.socket = this.openQQConnect();
+    } else if (CC_WECHATGAME) {
+        this.socket = this.openWxConnect();
     } else {
         this.socket = this.openH5Connect()
     }
@@ -237,10 +239,12 @@ WsManager.prototype.reconnect = function() {
         console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
         this.readyState = 'reconnecting'
         setTimeout(() => {
-            if (CC_WECHATGAME) {
-                this.socket = this.openWxConnect();
+            if (window.tt != undefined) {
+                this.socket = this.openTTConnect();
             } else if (CC_QQPLAY) {
                 this.socket = this.openQQConnect();
+            } else if (CC_WECHATGAME) {
+                this.socket = this.openWxConnect();
             } else {
                 this.socket = this.openH5Connect()
             }
@@ -339,6 +343,8 @@ WsManager.prototype.openWxConnect = function() {
     return _socket;
 }
 
+
+
 WsManager.prototype.openQQConnect = function() {
 
     let ws = new BK.WebSocket(this.url);
@@ -355,9 +361,10 @@ WsManager.prototype.openQQConnect = function() {
     ws.onClose = function(ws) {
         self.readyState = 'closed';
         
-        //只要关闭就重连(暂时性处理)
-        if(self._reconnection) {
-            self.reconnect();
+        clearInterval(this.keepAliveTimeout);
+        //只要关闭就重连(暂时性处理)并且不是在后台 不进行重连
+        if(this._reconnection && !GameGlobal.isOnHide ) {
+            this.reconnect();
         }
 
         self.emit('close', 'qq websocket关闭');
@@ -366,10 +373,12 @@ WsManager.prototype.openQQConnect = function() {
 
     
     ws.onError = function(ws) {
-        if(self._reconnection) {
-            self.reconnect()
+        this.readyState = 'closed';
+        clearInterval(this.keepAliveTimeout);
+        if(this._reconnection && !GameGlobal.isOnHide) {
+            this.reconnect();
         } else {
-            _socket.close()
+            ws.close();
         }
 
         self.emit('error', 'error')
@@ -391,6 +400,64 @@ WsManager.prototype.openQQConnect = function() {
     return ws;
 }
 
+//// 头条socket
+WsManager.prototype.openTTConnect = function() {
+    let socketTask = tt.connectSocket({
+        "url": this.url,
+    });
+    let self = this;
+
+    socketTask.onOpen(() => {
+        self.readyState = 'open';
+        self.emit('open', '打开TTwebSocket');
+        console.log(`%c [Socket连接成功: ${self.url.split("&token")[0]}]`, self.logStyle);
+        // 每隔一段时间发一个心跳包保持连接状态
+        self.keepAlive();
+    });
+    
+    socketTask.onClose(() => {
+        self.readyState = 'closed';
+        
+        clearInterval(this.keepAliveTimeout);
+        //只要关闭就重连(暂时性处理)并且不是在后台 不进行重连
+        if(this._reconnection && !GameGlobal.isOnHide ) {
+            this.reconnect();
+        }
+        self.emit('close', '头条 websocket关闭');
+        console.log(`%c [Socket连接关闭]`, self.logStyle);
+    });
+    
+    socketTask.onError(error => {
+        this.readyState = 'closed';
+        clearInterval(this.keepAliveTimeout);
+        if(this._reconnection && !GameGlobal.isOnHide) {
+            this.reconnect();
+        } else {
+            socketTask.close();
+        }
+
+        self.emit(error, 'error')
+        console.log(`%c [Socket错误] ${error}`, self.logStyle);
+    });
+    
+    socketTask.onMessage(message => {
+        console.log('socket message:', message);
+        let data = message.data;
+        if(data != 1) {
+            if (Object.prototype.toString.call(data) === '[object ArrayBuffer]') {
+                data = Codec.read(data);
+            }
+            this.emit('message', data)
+        }
+        GameGlobal._socketCount += 1;
+        if (GameGlobal._socketCount > 10000) {
+            GameGlobal._socketCount = 2;
+        }
+        console.log(`%c [接收到Socket消息: ${JSON.stringify(message.data)}]`, this.logStyle);
+    });
+    return socketTask;
+}
+
 WsManager.prototype.openH5Connect = function() {
     let _socket = new WebSocket(this.url);
     _socket.binaryType = "arraybuffer";