|
@@ -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) {
|
|
@@ -233,25 +232,34 @@ WsManager.prototype.reconnect = function() {
|
|
|
if(this.socket) {
|
|
|
this.socket.close()
|
|
|
}
|
|
|
+
|
|
|
this._reconnectTimes += 1
|
|
|
-
|
|
|
- console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
|
|
|
this.readyState = 'reconnecting'
|
|
|
+
|
|
|
+ console.log(`%c [Socket正在尝试第${this._reconnectTimes}次重连]`, this.logStyle);
|
|
|
+
|
|
|
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 = {
|
|
@@ -280,20 +288,26 @@ 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连接被关闭: ${res}]`, this.logStyle)
|
|
|
+
|
|
|
+ clearInterval(this.keepAliveTimeout)
|
|
|
+
|
|
|
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) => {
|
|
@@ -304,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';
|
|
@@ -314,7 +332,6 @@ WsManager.prototype.openWxConnect = function() {
|
|
|
}
|
|
|
|
|
|
this.emit('error', res.errMsg)
|
|
|
- console.log(`%c [Socket错误: ${res.errMsg}]`, this.logStyle);
|
|
|
})
|
|
|
|
|
|
return _socket
|
|
@@ -329,23 +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) => {
|
|
|
+ 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()
|
|
|
- // }
|
|
|
+ if(this._reconnection) {
|
|
|
+ this.reconnect()
|
|
|
+ }
|
|
|
|
|
|
this.emit('close', event)
|
|
|
- console.log(`%c [Socket连接关闭: ${reason}]`, this.logStyle)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
_socket.onmessage = (event) => {
|
|
@@ -356,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';
|
|
@@ -366,7 +394,6 @@ WsManager.prototype.openH5Connect = function() {
|
|
|
}
|
|
|
|
|
|
this.emit(event)
|
|
|
- console.log(`%c [Socket错误: ${JSON.stringify(event)}]`, this.logStyle);
|
|
|
}
|
|
|
return _socket
|
|
|
}
|