mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 09:52:50 +08:00
Streaming out of alpha; "Are you still there?" idle check
- Drop the ALPHA badge, the alpha warning in the stream dialog and the "(Alpha)" in the stream button's tooltip. - New IDLE_TIMEOUT_MINUTES (admin panel / .env, 0 = off). The gateway announces it in the hello; after that long without input, talking or broadcasting the page asks "Are you still there?" and disconnects all tabs if nobody answers within 60 s. The page sends a throttled "active" message, and the gateway closes sockets that stay silent for the timeout plus 3 minutes, for pages that never ask. The page treats that close as deliberate and doesn't reconnect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
fc33843179
commit
958095fbf4
10 files changed
+157
-68
No files matched your search
@@ -39,6 +39,7 @@ export const SETTINGS: Setting[] = [
|
||||
{ group: "Connect dialog", staticOnly: true, key: "DEFAULT_SERVER", kind: "text", help: "Server address prefilled in the connect dialog." },
|
||||
{ group: "Connect dialog", staticOnly: true, key: "DEFAULT_CHANNEL", kind: "text", help: "Channel prefilled in the connect dialog." },
|
||||
{ group: "Connect dialog", staticOnly: true, key: "SYNC_URL", kind: "text", help: "Own account-sync server (sync-server.js). Empty = the official one." },
|
||||
{ group: "Connections & limits", key: "IDLE_TIMEOUT_MINUTES", kind: "number", fallback: "0", help: "Ask \"Are you still there?\" after this many minutes without input in the client; no answer within 60 s disconnects (0 = off)." },
|
||||
{ group: "Features", key: "FEEDBACK_ENABLED", kind: "bool", fallback: "0", help: "Accept in-app feedback (/api/feedback)." },
|
||||
{ group: "Features", key: "STORE_ENABLED", kind: "bool", fallback: "0", help: "Run the Design Store (theme submissions and downloads)." },
|
||||
{ group: "Features", key: "LOG_CONNECTIONS", kind: "bool", fallback: "0", help: "Log connects/disconnects (hashed client tags, no plain IPs)." },
|
||||
|
||||
+32
-2
@@ -624,8 +624,17 @@ const HEARTBEAT_INTERVAL_MS = 20_000;
|
||||
|
||||
interface HeartbeatState {
|
||||
isAlive: boolean;
|
||||
/** Last "active" message (user input in the page) or connect. */
|
||||
lastActive: number;
|
||||
}
|
||||
|
||||
/** IDLE_TIMEOUT_MINUTES in ms, 0 = off. Read per check, the admin panel changes it live. */
|
||||
const idleTimeoutMs = () => Math.max(0, Number(process.env.IDLE_TIMEOUT_MINUTES) || 0) * 60_000;
|
||||
// The page asks "Are you still there?" after the timeout and waits 60 s; the
|
||||
// gateway only steps in after that plus slack, for pages that never asked
|
||||
// (outdated or modified). Throttled "active" messages keep it away.
|
||||
const IDLE_GRACE_MS = 3 * 60_000;
|
||||
|
||||
const heartbeats = new WeakMap<WebSocket, HeartbeatState>();
|
||||
|
||||
// Tracked so SIGTERM (below) can tell every live TS3/TeaSpeak session to
|
||||
@@ -647,6 +656,15 @@ const heartbeatTimer = setInterval(() => {
|
||||
socket.terminate();
|
||||
continue;
|
||||
}
|
||||
const idleMs = idleTimeoutMs();
|
||||
if (idleMs > 0 && Date.now() - state.lastActive > idleMs + IDLE_GRACE_MS) {
|
||||
// Close handler disconnects the TS session; the message tells the page
|
||||
// this was deliberate, so it doesn't auto-reconnect.
|
||||
socket.send('{"type":"idleTimeout"}');
|
||||
socket.close(4000, "idle");
|
||||
heartbeats.delete(socket);
|
||||
continue;
|
||||
}
|
||||
state.isAlive = false;
|
||||
socket.ping();
|
||||
// Protocol pings are invisible to browser JS; this lets the page notice
|
||||
@@ -730,9 +748,16 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
// pulled, so the browser must not assume a feature exists just because it
|
||||
// knows about it. A gateway without "hello" (before v0.15) supports the
|
||||
// v0.14 feature set, streaming included; anything newer gets a name here.
|
||||
socket.send(JSON.stringify({ type: "hello", features: gatewayFeatures(), iceServers: turnServers() }));
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "hello",
|
||||
features: gatewayFeatures(),
|
||||
iceServers: turnServers(),
|
||||
idleTimeoutMinutes: idleTimeoutMs() / 60_000,
|
||||
}),
|
||||
);
|
||||
|
||||
heartbeats.set(socket, { isAlive: true });
|
||||
heartbeats.set(socket, { isAlive: true, lastActive: Date.now() });
|
||||
socket.on("pong", () => {
|
||||
const state = heartbeats.get(socket);
|
||||
if (state) state.isAlive = true;
|
||||
@@ -764,6 +789,11 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
}
|
||||
|
||||
async function handleMessage(msg: any): Promise<void> {
|
||||
if (msg.type === "active" || msg.type === "connect") {
|
||||
const state = heartbeats.get(socket);
|
||||
if (state) state.lastActive = Date.now();
|
||||
if (msg.type === "active") return;
|
||||
}
|
||||
switch (msg.type) {
|
||||
case "connect": {
|
||||
if (!isValidHost(msg.host) || !isValidNickname(msg.nickname)) {
|
||||
|
||||
Reference in new issue
Block a user