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:
MoepchiandClaude Opus 5.5 committed 2026-11-30 23:28:33 +00:00
1 parent fc33843179
commit 958095fbf4
10 files changed
+157 -68

No files matched your search

+32 -2
View File
@@ -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)) {