WebSocket auto-reconnect: yhteys palautuu 3s kuluttua katkoksesta

connectHub() luo uuden WebSocketin ja asettaa onopen/onclose/onmessage.
onclose käynnistää 3s timerin joka kutsuu connectHub() uudelleen.
Terminaaliin tulee '↻ Yhdistetään uudelleen...' -viesti.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 07:14:52 +03:00
parent 216b95d15c
commit 73bcd3143a

View File

@@ -1672,9 +1672,19 @@
if (e.key === 'Enter') sendUserText(); if (e.key === 'Enter') sendUserText();
}); });
// Kytkemme sivuston UI-puolen (JS) omaan passiiviseen WebSocket-kuuntelijaan. // WebSocket-yhteys hubiin — automaattinen reconnect
const uiSocket = new WebSocket(`${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`); const wsUrl = `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}/ws`;
window._uiSocket = uiSocket; let uiSocket = null;
let wsReconnectTimer = null;
function connectHub() {
if (uiSocket && (uiSocket.readyState === 0 || uiSocket.readyState === 1)) return;
uiSocket = new WebSocket(wsUrl);
window._uiSocket = uiSocket;
// Kytketään onmessage uudelleen (handler määritellään myöhemmin, asetetaan kun valmis)
setTimeout(() => {
if (window._wsMessageHandler) uiSocket.onmessage = window._wsMessageHandler;
}, 0);
uiSocket.onopen = async () => { uiSocket.onopen = async () => {
// Päivitetään agents-näkymän hub-status // Päivitetään agents-näkymän hub-status
const hubDot = document.getElementById('agent-hub-dot'); const hubDot = document.getElementById('agent-hub-dot');
@@ -1746,7 +1756,18 @@
coderEl.textContent = 'Disconnected'; coderEl.textContent = 'Disconnected';
coderEl.style.color = '#f85149'; coderEl.style.color = '#f85149';
} }
// Automaattinen reconnect 3s kuluttua
if (!wsReconnectTimer) {
wsReconnectTimer = setTimeout(() => {
wsReconnectTimer = null;
termLog(' <span style="color:#d29922">↻ Yhdistetään uudelleen...</span>');
connectHub();
}, 3000);
}
}; };
} // connectHub()
connectHub();
// Terminaalin komentorivi // Terminaalin komentorivi
const termInput = document.getElementById('term-input'); const termInput = document.getElementById('term-input');
const termPanel = document.getElementById('agent-terminal'); const termPanel = document.getElementById('agent-terminal');
@@ -2753,7 +2774,8 @@ Files: ${Object.keys(generatedFiles).join(', ')}`;
// Klikkaa terminaalipaneelia → fokusoi input // Klikkaa terminaalipaneelia → fokusoi input
termPanel?.addEventListener('click', () => termInput?.focus()); termPanel?.addEventListener('click', () => termInput?.focus());
uiSocket.onmessage = (event) => { // Tallennetaan message-handler funktioon jotta reconnect voi käyttää samaa
const _wsHandler = (event) => {
try { try {
const raw = event.data; const raw = event.data;
if (raw.includes('"single_tokenize"')) return; if (raw.includes('"single_tokenize"')) return;
@@ -3115,6 +3137,8 @@ Files: ${Object.keys(generatedFiles).join(', ')}`;
} }
} catch(e) {} } catch(e) {}
}; };
window._wsMessageHandler = _wsHandler;
if (uiSocket) uiSocket.onmessage = _wsHandler;
btn.addEventListener('click', async () => { btn.addEventListener('click', async () => {
// Käytetään viewer-authissa jo tunnistettua WebGPU-tilaa // Käytetään viewer-authissa jo tunnistettua WebGPU-tilaa