From a96841043193df71676dc78eaead37eeb8769db9 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 28 Aug 2023 16:04:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=9E=E6=8E=A5=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/modules/sync/client/client.ts | 39 +++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/modules/sync/client/client.ts b/src/main/modules/sync/client/client.ts index 67855430..7421f8d9 100644 --- a/src/main/modules/sync/client/client.ts +++ b/src/main/modules/sync/client/client.ts @@ -35,6 +35,7 @@ const heartbeatTools = { failedNum: 0, maxTryNum: 100000, stepMs: 3000, + connectTimeout: null as NodeJS.Timeout | null, pingTimeout: null as NodeJS.Timeout | null, delayRetryTimeout: null as NodeJS.Timeout | null, handleOpen() { @@ -54,15 +55,16 @@ const heartbeatTools = { }, 30000 + 1000) }, reConnnect() { - if (this.pingTimeout) { - clearTimeout(this.pingTimeout) - this.pingTimeout = null - } + this.clearTimeout() // client = null if (!client) return if (++this.failedNum > this.maxTryNum) { this.failedNum = 0 + sendSyncStatus({ + status: false, + message: 'Connect error', + }) throw new Error('connect error') } @@ -85,6 +87,10 @@ const heartbeatTools = { }, waitTime) }, clearTimeout() { + if (this.connectTimeout) { + clearTimeout(this.connectTimeout) + this.connectTimeout = null + } if (this.delayRetryTimeout) { clearTimeout(this.delayRetryTimeout) this.delayRetryTimeout = null @@ -96,7 +102,32 @@ const heartbeatTools = { }, connect(socket: LX.Sync.Client.Socket) { 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', () => { + if (this.connectTimeout) { + clearTimeout(this.connectTimeout) + this.connectTimeout = null + } this.handleOpen() }) socket.on('ping', () => {