添加连接超时处理
parent
c6f4bfa875
commit
a968410431
|
@ -35,6 +35,7 @@ const heartbeatTools = {
|
||||||
failedNum: 0,
|
failedNum: 0,
|
||||||
maxTryNum: 100000,
|
maxTryNum: 100000,
|
||||||
stepMs: 3000,
|
stepMs: 3000,
|
||||||
|
connectTimeout: null as NodeJS.Timeout | null,
|
||||||
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() {
|
||||||
|
@ -54,15 +55,16 @@ const heartbeatTools = {
|
||||||
}, 30000 + 1000)
|
}, 30000 + 1000)
|
||||||
},
|
},
|
||||||
reConnnect() {
|
reConnnect() {
|
||||||
if (this.pingTimeout) {
|
this.clearTimeout()
|
||||||
clearTimeout(this.pingTimeout)
|
|
||||||
this.pingTimeout = null
|
|
||||||
}
|
|
||||||
// client = null
|
// client = null
|
||||||
if (!client) return
|
if (!client) return
|
||||||
|
|
||||||
if (++this.failedNum > this.maxTryNum) {
|
if (++this.failedNum > this.maxTryNum) {
|
||||||
this.failedNum = 0
|
this.failedNum = 0
|
||||||
|
sendSyncStatus({
|
||||||
|
status: false,
|
||||||
|
message: 'Connect error',
|
||||||
|
})
|
||||||
throw new Error('connect error')
|
throw new Error('connect error')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +87,10 @@ const heartbeatTools = {
|
||||||
}, waitTime)
|
}, waitTime)
|
||||||
},
|
},
|
||||||
clearTimeout() {
|
clearTimeout() {
|
||||||
|
if (this.connectTimeout) {
|
||||||
|
clearTimeout(this.connectTimeout)
|
||||||
|
this.connectTimeout = null
|
||||||
|
}
|
||||||
if (this.delayRetryTimeout) {
|
if (this.delayRetryTimeout) {
|
||||||
clearTimeout(this.delayRetryTimeout)
|
clearTimeout(this.delayRetryTimeout)
|
||||||
this.delayRetryTimeout = null
|
this.delayRetryTimeout = null
|
||||||
|
@ -96,7 +102,32 @@ const heartbeatTools = {
|
||||||
},
|
},
|
||||||
connect(socket: LX.Sync.Client.Socket) {
|
connect(socket: LX.Sync.Client.Socket) {
|
||||||
console.log('heartbeatTools connect')
|
console.log('heartbeatTools connect')
|
||||||
|
this.connectTimeout = setTimeout(() => {
|
||||||
|
this.connectTimeout = null
|
||||||
|
if (client) {
|
||||||
|
try {
|
||||||
|
client.close(SYNC_CLOSE_CODE.failed)
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
if (++this.failedNum > this.maxTryNum) {
|
||||||
|
this.failedNum = 0
|
||||||
|
sendSyncStatus({
|
||||||
|
status: false,
|
||||||
|
message: 'Connect error',
|
||||||
|
})
|
||||||
|
throw new Error('connect error')
|
||||||
|
}
|
||||||
|
sendSyncStatus({
|
||||||
|
status: false,
|
||||||
|
message: 'Connect timeout, try reconnect...',
|
||||||
|
})
|
||||||
|
this.reConnnect()
|
||||||
|
}, 2 * 60 * 1000)
|
||||||
socket.on('open', () => {
|
socket.on('open', () => {
|
||||||
|
if (this.connectTimeout) {
|
||||||
|
clearTimeout(this.connectTimeout)
|
||||||
|
this.connectTimeout = null
|
||||||
|
}
|
||||||
this.handleOpen()
|
this.handleOpen()
|
||||||
})
|
})
|
||||||
socket.on('ping', () => {
|
socket.on('ping', () => {
|
||||||
|
|
Loading…
Reference in New Issue