From 6182a1cb2c80cce573a17193e9309fbbbce3b08c Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 9 Jun 2023 17:07:57 +0900 Subject: [PATCH] =?UTF-8?q?enhance(backend):=20WebSocket=E3=81=AEPing/Pong?= =?UTF-8?q?=E3=82=92=E3=83=97=E3=83=AD=E3=83=88=E3=82=B3=E3=83=AB=E5=88=B6?= =?UTF-8?q?=E5=BE=A1=E3=83=95=E3=83=AC=E3=83=BC=E3=83=A0=E3=81=AE=E7=89=A9?= =?UTF-8?q?=E3=81=A7=E5=88=A4=E5=88=A5=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolve #10969 --- .../src/server/api/StreamingApiServerService.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/server/api/StreamingApiServerService.ts b/packages/backend/src/server/api/StreamingApiServerService.ts index 2e554c9ad1..d1394d6d76 100644 --- a/packages/backend/src/server/api/StreamingApiServerService.ts +++ b/packages/backend/src/server/api/StreamingApiServerService.ts @@ -132,11 +132,8 @@ export class StreamingApiServerService { if (userUpdateIntervalId) clearInterval(userUpdateIntervalId); }); - connection.on('message', async (data) => { + connection.on('pong', () => { this.#connections.set(connection, Date.now()); - if (data.toString() === 'ping') { - connection.send('pong'); - } }); }); @@ -144,12 +141,14 @@ export class StreamingApiServerService { this.#cleanConnectionsIntervalId = setInterval(() => { const now = Date.now(); for (const [connection, lastActive] of this.#connections.entries()) { - if (now - lastActive > 1000 * 60 * 5) { + if (now - lastActive > 1000 * 60 * 2) { connection.terminate(); this.#connections.delete(connection); + } else { + connection.ping(); } } - }, 1000 * 60 * 5); + }, 1000 * 60); } @bindThis