From 734da781f8fda5797665855317129a60df2909c8 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 27 Jun 2026 20:56:34 +0200 Subject: [PATCH] Fix browser client WS connection: missing brace, error handling, localhost fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing the Enabled else-if branch in a previous commit left the Soll if-block unclosed, crashing app.js at parse time so no JS ran at all. Also adds: onerror handler surfaces connection failures in the status label; try/catch for invalid URL; 'Connecting…' feedback; localhost fallback when location.hostname is empty (file:// open); wider host input field. Co-Authored-By: Claude Sonnet 4.6 Claude-Session: https://claude.ai/code/session_01DF16nV2ispNw1C9SbvqbRQ --- web/app.js | 21 ++++++++++++++++++--- web/style.css | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/web/app.js b/web/app.js index 1af6002..72ccce5 100644 --- a/web/app.js +++ b/web/app.js @@ -588,6 +588,7 @@ function onTempCtrlChanged(msg) { initialHeatrateSync = false; } } + } if (key === 'Ist') { const ist = msg.Ist; if ('Temp' in ist) { @@ -726,8 +727,18 @@ function setConnected(isConnected) { } function connect() { - const host = document.getElementById('host').value; - ws = new WebSocket(host); + const statusEl = document.getElementById('conn-status'); + const url = document.getElementById('host').value.trim(); + let ws_; + try { + ws_ = new WebSocket(url); + } catch (e) { + statusEl.textContent = `Invalid host: ${e.message}`; + statusEl.className = 'status-disconnected'; + return; + } + ws = ws_; + statusEl.textContent = 'Connecting…'; ws.onopen = () => { setConnected(true); for (const channel of CHANNELS) { @@ -739,6 +750,10 @@ function connect() { // (see updateSudForecast()'s isNewSchedule check). sendMsg('Sud', {Save: true}); }; + ws.onerror = () => { + statusEl.textContent = `Could not connect to ${url}`; + statusEl.className = 'status-disconnected'; + }; ws.onclose = () => setConnected(false); ws.onmessage = (event) => { const msg = JSON.parse(event.data); @@ -759,7 +774,7 @@ function disconnect() { // --- DOM wiring. --- -document.getElementById('host').value = `ws://${location.hostname}:8765`; +document.getElementById('host').value = `ws://${location.hostname || 'localhost'}:8765`; document.getElementById('btn-connect').addEventListener('click', () => { if (connected) { diff --git a/web/style.css b/web/style.css index 30387ab..af5b689 100644 --- a/web/style.css +++ b/web/style.css @@ -69,6 +69,7 @@ header#connection-bar { padding: 0.6em 0; border-bottom: 1px solid var(--border); } +#host { width: 18em; } .status-connected { color: var(--green); font-weight: bold; } .status-disconnected { color: var(--text-muted); }