fix: 🐛 client sends ping to keep websocket connection alive

This commit is contained in:
2026-05-14 11:56:33 +00:00
parent 20db4742a7
commit 90103f79c8
2 changed files with 18 additions and 0 deletions
+3
View File
@@ -1290,6 +1290,9 @@ def ws_terminal():
height=int(o.get("rows", 40)), height=int(o.get("rows", 40)),
) )
return True return True
elif o.get("type") == "ping":
# Ping message to keep connection alive, ignore without sending to channel
return True
except (json.JSONDecodeError, TypeError, ValueError): except (json.JSONDecodeError, TypeError, ValueError):
pass pass
channel.send(msg.encode("utf-8")) channel.send(msg.encode("utf-8"))
+15
View File
@@ -24,6 +24,7 @@ let ws: WebSocket | null = null;
let term: Terminal | null = null; let term: Terminal | null = null;
let fit: FitAddon | null = null; let fit: FitAddon | null = null;
let ro: ResizeObserver | null = null; let ro: ResizeObserver | null = null;
let pingInterval: NodeJS.Timeout | null = null;
function wsUrl(hostId: number): string { function wsUrl(hostId: number): string {
const proto = location.protocol === "https:" ? "wss:" : "ws:"; const proto = location.protocol === "https:" ? "wss:" : "ws:";
@@ -86,6 +87,12 @@ onMounted(async () => {
ws.onopen = () => { ws.onopen = () => {
status.value = "Handshaking…"; status.value = "Handshaking…";
sendResize(); sendResize();
// Send ping every 60 seconds to keep connection alive
pingInterval = setInterval(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "ping" }));
}
}, 60000);
}; };
ws.onmessage = (ev) => { ws.onmessage = (ev) => {
@@ -115,6 +122,10 @@ onMounted(async () => {
}; };
ws.onclose = () => { ws.onclose = () => {
if (pingInterval) {
clearInterval(pingInterval);
pingInterval = null;
}
if (!connId.value) { if (!connId.value) {
status.value = "Disconnected"; status.value = "Disconnected";
} else { } else {
@@ -124,6 +135,10 @@ onMounted(async () => {
}); });
onUnmounted(() => { onUnmounted(() => {
if (pingInterval) {
clearInterval(pingInterval);
pingInterval = null;
}
ro?.disconnect(); ro?.disconnect();
ro = null; ro = null;
ws?.close(); ws?.close();