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