优化自动重连
parent
fe0487c3f9
commit
7f008d1f9c
|
@ -38,12 +38,13 @@ const handleConnection = (socket: LX.Sync.Client.Socket) => {
|
||||||
|
|
||||||
const heartbeatTools = {
|
const heartbeatTools = {
|
||||||
failedNum: 0,
|
failedNum: 0,
|
||||||
maxTryNum: 3,
|
maxTryNum: 100000,
|
||||||
|
stepMs: 3000,
|
||||||
pingTimeout: null as NodeJS.Timeout | null,
|
pingTimeout: null as NodeJS.Timeout | null,
|
||||||
delayRetryTimeout: null as NodeJS.Timeout | null,
|
delayRetryTimeout: null as NodeJS.Timeout | null,
|
||||||
handleOpen() {
|
handleOpen() {
|
||||||
console.log('open')
|
console.log('open')
|
||||||
this.failedNum = 0
|
// this.failedNum = 0
|
||||||
this.heartbeat()
|
this.heartbeat()
|
||||||
},
|
},
|
||||||
heartbeat() {
|
heartbeat() {
|
||||||
|
@ -70,16 +71,23 @@ const heartbeatTools = {
|
||||||
throw new Error('connect error')
|
throw new Error('connect error')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const waitTime = Math.min(2000 + Math.floor(this.failedNum / 2) * this.stepMs, 15000)
|
||||||
|
|
||||||
|
// sendSyncStatus({
|
||||||
|
// status: false,
|
||||||
|
// message: `Waiting ${waitTime / 1000}s reconnnect...`,
|
||||||
|
// })
|
||||||
|
|
||||||
this.delayRetryTimeout = setTimeout(() => {
|
this.delayRetryTimeout = setTimeout(() => {
|
||||||
this.delayRetryTimeout = null
|
this.delayRetryTimeout = null
|
||||||
if (!client) return
|
if (!client) return
|
||||||
console.log(dateFormat(new Date()), 'reconnnect...')
|
console.log(dateFormat(new Date()), 'reconnnect...')
|
||||||
sendSyncStatus({
|
sendSyncStatus({
|
||||||
status: false,
|
status: false,
|
||||||
message: `Try reconnnect... (${this.failedNum}/${this.maxTryNum})`,
|
message: `Try reconnnect... (${this.failedNum})`,
|
||||||
})
|
})
|
||||||
connect(client.data.urlInfo, client.data.keyInfo)
|
connect(client.data.urlInfo, client.data.keyInfo)
|
||||||
}, 2000)
|
}, waitTime)
|
||||||
},
|
},
|
||||||
clearTimeout() {
|
clearTimeout() {
|
||||||
if (this.delayRetryTimeout) {
|
if (this.delayRetryTimeout) {
|
||||||
|
@ -177,6 +185,7 @@ export const connect = (urlInfo: LX.Sync.Client.UrlInfo, keyInfo: LX.Sync.Client
|
||||||
handleConnection(client as LX.Sync.Client.Socket)
|
handleConnection(client as LX.Sync.Client.Socket)
|
||||||
log.info('register list sync service success')
|
log.info('register list sync service success')
|
||||||
client!.isReady = true
|
client!.isReady = true
|
||||||
|
heartbeatTools.failedNum = 0
|
||||||
sendSyncStatus({
|
sendSyncStatus({
|
||||||
status: true,
|
status: true,
|
||||||
message: '',
|
message: '',
|
||||||
|
|
Loading…
Reference in New Issue