Add heartbeat timeout to detect server disconnection

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2026-01-27 11:23:00 -08:00
parent d173e8bac6
commit 17d7889d54

View File

@@ -1576,8 +1576,19 @@
function connectSSE() { function connectSSE() {
const evtSource = new EventSource('/api/status/stream'); const evtSource = new EventSource('/api/status/stream');
let heartbeatTimeout = null;
function resetHeartbeat() {
if (heartbeatTimeout) clearTimeout(heartbeatTimeout);
heartbeatTimeout = setTimeout(() => {
setConnectionStatus(false);
evtSource.close();
setTimeout(connectSSE, 2000);
}, 10000);
}
evtSource.addEventListener('status', async (event) => { evtSource.addEventListener('status', async (event) => {
resetHeartbeat();
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
if (!currentConfig) { if (!currentConfig) {
const configResp = await fetch('/api/config'); const configResp = await fetch('/api/config');
@@ -1588,9 +1599,11 @@
evtSource.onopen = () => { evtSource.onopen = () => {
setConnectionStatus(true); setConnectionStatus(true);
resetHeartbeat();
}; };
evtSource.onerror = () => { evtSource.onerror = () => {
if (heartbeatTimeout) clearTimeout(heartbeatTimeout);
setConnectionStatus(false); setConnectionStatus(false);
evtSource.close(); evtSource.close();
setTimeout(connectSSE, 2000); setTimeout(connectSSE, 2000);