|
@@ -4,12 +4,11 @@ import { getObjValue, isNullES } from 'js-fast-way'
|
|
|
// 长链接推送插件
|
|
|
class HcSocket {
|
|
|
|
|
|
- constructor(data, change, open) {
|
|
|
+ constructor(data, change) {
|
|
|
this.limit = website.socketLimit
|
|
|
this.interval = website.socketInterval
|
|
|
this.param = getObjValue(data)
|
|
|
this.onChange = change
|
|
|
- this.onSocketOpen = open
|
|
|
this.socket = null
|
|
|
this.isReconnecting = false
|
|
|
this.connectionStatus = 'disconnected'
|
|
@@ -44,9 +43,6 @@ class HcSocket {
|
|
|
console.log('WebSocket 链接成功')
|
|
|
this.connectionStatus = 'connected'
|
|
|
this.isManualClosed = false
|
|
|
- if (typeof this.onSocketOpen === 'function' && this.onSocketOpen) {
|
|
|
- this.onSocketOpen()
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
handleClose() {
|
|
@@ -95,7 +91,7 @@ class HcSocket {
|
|
|
while (attempts < this.limit && !this.isSocket() && !this.isManualClosed) {
|
|
|
console.log(`重新链接中... (${attempts + 1}/${this.limit})`)
|
|
|
this.create()
|
|
|
- await new Promise(resolve => setTimeout(resolve, this.interval))
|
|
|
+ await new Promise(resolve => setTimeout(resolve, this.getReconnectInterval(attempts)))
|
|
|
attempts++
|
|
|
}
|
|
|
|
|
@@ -105,6 +101,11 @@ class HcSocket {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 指数退避策略
|
|
|
+ getReconnectInterval(attempt) {
|
|
|
+ return Math.min(1000 * Math.pow(2, attempt), this.interval)
|
|
|
+ }
|
|
|
+
|
|
|
handleMultipleTabs() {
|
|
|
document.addEventListener('visibilitychange', this.handleVisibilityChange.bind(this))
|
|
|
}
|