From d7f9615a8e4a5b41b38f0053cdd3ec63e7705000 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Mon, 10 Apr 2023 14:54:47 +0700 Subject: [PATCH] TSK-1092: Fix reconnect for Safari (#2929) Signed-off-by: Andrey Sobolev --- plugins/client-resources/src/connection.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/client-resources/src/connection.ts b/plugins/client-resources/src/connection.ts index 727a5c97d1..2c695890ba 100644 --- a/plugins/client-resources/src/connection.ts +++ b/plugins/client-resources/src/connection.ts @@ -68,6 +68,8 @@ class Connection implements ClientConnection { private readonly sessionId = generateId() as string private closed = false + private pingResponse: number = Date.now() + constructor ( private readonly url: string, private readonly handler: TxHandler, @@ -78,7 +80,25 @@ class Connection implements ClientConnection { console.log('connection created') this.interval = setInterval(() => { // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.sendRequest({ method: 'ping', params: [] }) + + if (this.pingResponse !== 0 && Date.now() - this.pingResponse > 3 * pingTimeout) { + // No ping response from server. + const s = this.websocket + + if (!(s instanceof Promise)) { + console.log('no ping response from server. Closing socket.', s, (s as any).readyState) + // Trying to close connection and re-establish it. + s?.close() + } else { + console.log('no ping response from server. Closing socket.', s) + void s.then((s) => s.close()) + } + this.websocket = null + } + + void this.sendRequest({ method: 'ping', params: [] }).then(() => { + this.pingResponse = Date.now() + }) }, pingTimeout) }