front-end: remember the selected station, reconnect after server restart

- Persist a manually picked station in localStorage (falls back to the
  station detected from the client IP).
- When the WebSocket closes, wait for the server to accept connections
  again and reload; only send pings while the socket is open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 03:00:37 +00:00
co-authored by Claude Opus 5.5
parent 1fa6f2251c
commit 404cc5fb6c
+37 -3
View File
@@ -527,7 +527,8 @@
this.stations = init.stations; this.stations = init.stations;
this.stationRadio = init.stationRadio; this.stationRadio = init.stationRadio;
this.selectedStation = init.clientStation; const stored = loadSelectedStation();
this.selectedStation = (stored && this.stations[stored]) ? stored : init.clientStation;
for (const [label, rotState] of Object.entries(init.rotators)) { for (const [label, rotState] of Object.entries(init.rotators)) {
const state = new RotatorState(); const state = new RotatorState();
@@ -593,6 +594,7 @@
pill.addEventListener('click', () => { pill.addEventListener('click', () => {
this.selectedStation = name; this.selectedStation = name;
saveSelectedStation(name);
this.updateStationSelection(); this.updateStationSelection();
this.applyAutoTab(); this.applyAutoTab();
}); });
@@ -724,9 +726,14 @@
else this.handleMessage(msg); else this.handleMessage(msg);
}); });
setInterval(() => { const pingTimer = setInterval(() => {
ws.send(JSON.stringify({ ping: 1 })); if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({ ping: 1 }));
}, 2000); }, 2000);
ws.addEventListener("close", () => {
clearInterval(pingTimer);
reloadWhenServerIsBack();
});
}); });
} }
} }
@@ -735,11 +742,38 @@
return location.origin.replace(/^http/, 'ws') + location.pathname.replace(/\/?$/, '/') + 'ws'; return location.origin.replace(/^http/, 'ws') + location.pathname.replace(/\/?$/, '/') + 'ws';
} }
/** Polls until the server accepts a WebSocket again, then reloads to get a fresh state. */
function reloadWhenServerIsBack() {
setTimeout(() => {
const probe = new WebSocket(wsUrl());
probe.addEventListener("open", () => location.reload());
probe.addEventListener("error", () => reloadWhenServerIsBack());
}, 2000);
}
/** N1MM frequencies are in tens of Hz. */ /** N1MM frequencies are in tens of Hz. */
function formatFreq(freq) { function formatFreq(freq) {
return (freq / 100000).toFixed(3) + ' MHz'; return (freq / 100000).toFixed(3) + ' MHz';
} }
const STATION_STORAGE_KEY = 'rotrouter.selectedStation';
function loadSelectedStation() {
try {
return localStorage.getItem(STATION_STORAGE_KEY);
} catch {
return null;
}
}
function saveSelectedStation(name) {
try {
localStorage.setItem(STATION_STORAGE_KEY, name);
} catch {
// storage unavailable (private mode etc.) — selection just won't persist
}
}
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
new RotatorApp().initialize(); new RotatorApp().initialize();
}); });