mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-03 14:52:09 +08:00
添加连接超时处理
This commit is contained in:
parent
5a69be8c7b
commit
9775e7dca6
@ -31,6 +31,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() {
|
||||||
@ -50,15 +51,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')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +83,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
|
||||||
@ -92,7 +98,32 @@ const heartbeatTools = {
|
|||||||
},
|
},
|
||||||
connect(socket: LX.Sync.Socket) {
|
connect(socket: LX.Sync.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.addEventListener('open', () => {
|
socket.addEventListener('open', () => {
|
||||||
|
if (this.connectTimeout) {
|
||||||
|
clearTimeout(this.connectTimeout)
|
||||||
|
this.connectTimeout = null
|
||||||
|
}
|
||||||
this.handleOpen()
|
this.handleOpen()
|
||||||
})
|
})
|
||||||
socket.addEventListener('message', ({ data }) => {
|
socket.addEventListener('message', ({ data }) => {
|
||||||
@ -128,7 +159,7 @@ export const connect = (urlInfo: LX.Sync.UrlInfo, keyInfo: LX.Sync.KeyInfo) => {
|
|||||||
funcsObj: {
|
funcsObj: {
|
||||||
...callObj,
|
...callObj,
|
||||||
finished() {
|
finished() {
|
||||||
toast('Sync connected.')
|
toast('Sync connected')
|
||||||
client!.isReady = true
|
client!.isReady = true
|
||||||
sendSyncStatus({
|
sendSyncStatus({
|
||||||
status: true,
|
status: true,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user