|
@@ -215,10 +215,12 @@ WsManager.prototype.connect = function(fn) {
|
|
}
|
|
}
|
|
|
|
|
|
this.readyState = 'opening';
|
|
this.readyState = 'opening';
|
|
- if (CC_WECHATGAME) {
|
|
|
|
- this.socket = this.openWxConnect();
|
|
|
|
|
|
+ if (window.tt != undefined) {
|
|
|
|
+ this.socket = this.openTTConnect();
|
|
} else if (CC_QQPLAY) {
|
|
} else if (CC_QQPLAY) {
|
|
this.socket = this.openQQConnect();
|
|
this.socket = this.openQQConnect();
|
|
|
|
+ } else if (CC_WECHATGAME) {
|
|
|
|
+ this.socket = this.openWxConnect();
|
|
} else {
|
|
} else {
|
|
this.socket = this.openH5Connect()
|
|
this.socket = this.openH5Connect()
|
|
}
|
|
}
|
|
@@ -237,10 +239,12 @@ WsManager.prototype.reconnect = function() {
|
|
console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
|
|
console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
|
|
this.readyState = 'reconnecting'
|
|
this.readyState = 'reconnecting'
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
- if (CC_WECHATGAME) {
|
|
|
|
- this.socket = this.openWxConnect();
|
|
|
|
|
|
+ if (window.tt != undefined) {
|
|
|
|
+ this.socket = this.openTTConnect();
|
|
} else if (CC_QQPLAY) {
|
|
} else if (CC_QQPLAY) {
|
|
this.socket = this.openQQConnect();
|
|
this.socket = this.openQQConnect();
|
|
|
|
+ } else if (CC_WECHATGAME) {
|
|
|
|
+ this.socket = this.openWxConnect();
|
|
} else {
|
|
} else {
|
|
this.socket = this.openH5Connect()
|
|
this.socket = this.openH5Connect()
|
|
}
|
|
}
|
|
@@ -339,6 +343,8 @@ WsManager.prototype.openWxConnect = function() {
|
|
return _socket;
|
|
return _socket;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
WsManager.prototype.openQQConnect = function() {
|
|
WsManager.prototype.openQQConnect = function() {
|
|
|
|
|
|
let ws = new BK.WebSocket(this.url);
|
|
let ws = new BK.WebSocket(this.url);
|
|
@@ -355,9 +361,10 @@ WsManager.prototype.openQQConnect = function() {
|
|
ws.onClose = function(ws) {
|
|
ws.onClose = function(ws) {
|
|
self.readyState = 'closed';
|
|
self.readyState = 'closed';
|
|
|
|
|
|
- //只要关闭就重连(暂时性处理)
|
|
|
|
- if(self._reconnection) {
|
|
|
|
- self.reconnect();
|
|
|
|
|
|
+ clearInterval(this.keepAliveTimeout);
|
|
|
|
+ //只要关闭就重连(暂时性处理)并且不是在后台 不进行重连
|
|
|
|
+ if(this._reconnection && !GameGlobal.isOnHide ) {
|
|
|
|
+ this.reconnect();
|
|
}
|
|
}
|
|
|
|
|
|
self.emit('close', 'qq websocket关闭');
|
|
self.emit('close', 'qq websocket关闭');
|
|
@@ -366,10 +373,12 @@ WsManager.prototype.openQQConnect = function() {
|
|
|
|
|
|
|
|
|
|
ws.onError = function(ws) {
|
|
ws.onError = function(ws) {
|
|
- if(self._reconnection) {
|
|
|
|
- self.reconnect()
|
|
|
|
|
|
+ this.readyState = 'closed';
|
|
|
|
+ clearInterval(this.keepAliveTimeout);
|
|
|
|
+ if(this._reconnection && !GameGlobal.isOnHide) {
|
|
|
|
+ this.reconnect();
|
|
} else {
|
|
} else {
|
|
- _socket.close()
|
|
|
|
|
|
+ ws.close();
|
|
}
|
|
}
|
|
|
|
|
|
self.emit('error', 'error')
|
|
self.emit('error', 'error')
|
|
@@ -391,6 +400,64 @@ WsManager.prototype.openQQConnect = function() {
|
|
return ws;
|
|
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() {
|
|
WsManager.prototype.openH5Connect = function() {
|
|
let _socket = new WebSocket(this.url);
|
|
let _socket = new WebSocket(this.url);
|
|
_socket.binaryType = "arraybuffer";
|
|
_socket.binaryType = "arraybuffer";
|