Fix browser client WS connection: missing brace, error handling, localhost fallback

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DF16nV2ispNw1C9SbvqbRQ
This commit is contained in:
2026-06-27 20:56:34 +02:00
co-authored by Claude Sonnet 4.6
parent dd6193fa46
commit 734da781f8
2 changed files with 19 additions and 3 deletions
+18 -3
View File
@@ -588,6 +588,7 @@ function onTempCtrlChanged(msg) {
initialHeatrateSync = false; initialHeatrateSync = false;
} }
} }
}
if (key === 'Ist') { if (key === 'Ist') {
const ist = msg.Ist; const ist = msg.Ist;
if ('Temp' in ist) { if ('Temp' in ist) {
@@ -726,8 +727,18 @@ function setConnected(isConnected) {
} }
function connect() { function connect() {
const host = document.getElementById('host').value; const statusEl = document.getElementById('conn-status');
ws = new WebSocket(host); 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 = () => { ws.onopen = () => {
setConnected(true); setConnected(true);
for (const channel of CHANNELS) { for (const channel of CHANNELS) {
@@ -739,6 +750,10 @@ function connect() {
// (see updateSudForecast()'s isNewSchedule check). // (see updateSudForecast()'s isNewSchedule check).
sendMsg('Sud', {Save: true}); sendMsg('Sud', {Save: true});
}; };
ws.onerror = () => {
statusEl.textContent = `Could not connect to ${url}`;
statusEl.className = 'status-disconnected';
};
ws.onclose = () => setConnected(false); ws.onclose = () => setConnected(false);
ws.onmessage = (event) => { ws.onmessage = (event) => {
const msg = JSON.parse(event.data); const msg = JSON.parse(event.data);
@@ -759,7 +774,7 @@ function disconnect() {
// --- DOM wiring. --- // --- 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', () => { document.getElementById('btn-connect').addEventListener('click', () => {
if (connected) { if (connected) {
+1
View File
@@ -69,6 +69,7 @@ header#connection-bar {
padding: 0.6em 0; padding: 0.6em 0;
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
} }
#host { width: 18em; }
.status-connected { color: var(--green); font-weight: bold; } .status-connected { color: var(--green); font-weight: bold; }
.status-disconnected { color: var(--text-muted); } .status-disconnected { color: var(--text-muted); }