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:
+18
-3
@@ -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) {
|
||||
|
||||
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user