|
@@ -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) => {
|