fix: 🐛 client sends ping to keep websocket connection alive
This commit is contained in:
@@ -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"))
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user