mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Reconnect resilience: gateway heartbeat + session auto-reconnect
Two related bugs: a browser tab that lost network (or got backgrounded/ suspended on Android) could leave the connector alive on the TS3/TeaSpeak server for minutes after the frontend showed disconnected, and multi-join background tabs simply vanished on any drop instead of staying connected. - gateway: WebSocket ping/pong heartbeat (20s) so a dead browser socket is detected and its connector torn down promptly instead of waiting on an OS-level TCP timeout. - web: every session (active tab or parked multi-join background tab) now remembers its connect params and can reopen its own socket after an unexpected drop, with exponential backoff (1s-30s) so a persistently unreachable gateway isn't hammered. Wake Lock keeps the screen (and the tab's throttling) at bay while any session is live; a visibilitychange sweep reconnects every dropped session once the page is visible again. Mic stays off for background sessions throughout, same as before.
This commit is contained in:
1 parent
fd5a4cd6e0
commit
20d1944dc9
2 files changed
+275
-54
No files matched your search
@@ -86,6 +86,36 @@ const server = createServer((req, res) => {
|
||||
|
||||
const wss = new WebSocketServer({ server, path: "/ws" });
|
||||
|
||||
// Heartbeat: a browser tab that loses its network mid-session (mobile
|
||||
// handover, Wi-Fi drop, backgrounded/frozen tab) often never sends a proper
|
||||
// WS close frame — the OS-level TCP timeout that would eventually notice can
|
||||
// take minutes, during which the connector child process (and the TS3/TeaSpeak
|
||||
// session it holds open) stays alive even though the frontend has already
|
||||
// given up and shown "disconnected". Standard `ws` ping/pong liveness check
|
||||
// closes those zombie sockets (and their connector) promptly instead.
|
||||
const HEARTBEAT_INTERVAL_MS = 20_000;
|
||||
|
||||
interface HeartbeatState {
|
||||
isAlive: boolean;
|
||||
}
|
||||
|
||||
const heartbeats = new WeakMap<WebSocket, HeartbeatState>();
|
||||
|
||||
const heartbeatTimer = setInterval(() => {
|
||||
for (const socket of wss.clients) {
|
||||
const state = heartbeats.get(socket);
|
||||
if (!state) continue;
|
||||
if (!state.isAlive) {
|
||||
socket.terminate();
|
||||
continue;
|
||||
}
|
||||
state.isAlive = false;
|
||||
socket.ping();
|
||||
}
|
||||
}, HEARTBEAT_INTERVAL_MS);
|
||||
|
||||
wss.on("close", () => clearInterval(heartbeatTimer));
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`WebSpeak3 gateway listening on http://localhost:${PORT} (WebSocket at /ws)`);
|
||||
if (SERVE_STATIC) {
|
||||
@@ -101,6 +131,12 @@ wss.on("connection", (socket: WebSocket) => {
|
||||
// multiple /ws connections in parallel (one per server tab).
|
||||
let connection: Ts3Connection | undefined;
|
||||
|
||||
heartbeats.set(socket, { isAlive: true });
|
||||
socket.on("pong", () => {
|
||||
const state = heartbeats.get(socket);
|
||||
if (state) state.isAlive = true;
|
||||
});
|
||||
|
||||
socket.on("message", async (raw) => {
|
||||
const msg = JSON.parse(raw.toString());
|
||||
|
||||
@@ -338,6 +374,7 @@ wss.on("connection", (socket: WebSocket) => {
|
||||
});
|
||||
|
||||
socket.on("close", () => {
|
||||
heartbeats.delete(socket);
|
||||
connection?.disconnect();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user