Skip to content

Commit

Permalink
enhance(backend): WebSocketのPing/Pongをプロトコル制御フレームの物で判別する
Browse files Browse the repository at this point in the history
Resolve #10969
  • Loading branch information
syuilo committed Jun 9, 2023
1 parent 308ab8f commit 6182a1c
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/backend/src/server/api/StreamingApiServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,23 @@ 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');
}
});
});

// 一定期間通信が無いコネクションは実際には切断されている可能性があるため定期的にterminateする
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
Expand Down

0 comments on commit 6182a1c

Please sign in to comment.