fix radar web

This commit is contained in:
admin committed 2026-09-11 22:09:39 +07:00
1 parent f5b4ef4e90
commit 8f9a7bae62
3 files changed
+139 -39

No files matched your search

+49 -15
View File
@@ -664,38 +664,67 @@ function collectEditorPayload() {
return zones;
}
/* ---------- WebSocket to backend ---------- */
/* ---------- WebSocket to backend + Polling fallback ---------- */
let wsReconnectTimer = null;
let fallbackPollTimer = null;
let _activeWs = null;
async function pollFallbackOnce() {
/* Try REST /api/state as a safety net even while WebSocket might be blocked */
try {
const r = await fetch("/api/state", { cache: "no-store" });
if (r.ok) {
const d = await r.json();
onBackendFrame(d);
}
} catch (_) {}
}
function startFallbackPoller() {
if (fallbackPollTimer) return;
/* Every 2 s also poll via REST, to cover browser cases where WS silently drops */
fallbackPollTimer = setInterval(pollFallbackOnce, 2000);
}
function connectWS() {
const proto = (location.protocol === "https:") ? "wss" : "ws";
const url = `${proto}://${location.host}/ws`;
updateWsStatus("connecting");
console.info("[Radar] Connecting WebSocket:", url);
let ws;
try { ws = new WebSocket(url); }
catch (e) { scheduleReconnect(); return; }
try {
_activeWs = new WebSocket(url);
} catch (e) {
console.warn("[Radar] WebSocket construct error:", e);
scheduleReconnect();
return;
}
ws.onopen = () => {
_activeWs.onopen = () => {
State.browserWsConnected = true;
updateWsStatus("ok");
console.info("[Radar] WebSocket connected");
/* Ask for first state immediately via REST too, in case the WS initial send was lost */
pollFallbackOnce();
};
ws.onmessage = (ev) => {
_activeWs.onmessage = (ev) => {
try {
const data = JSON.parse(ev.data);
onBackendFrame(data);
} catch (e) { console.warn("WS parse error", e); }
} catch (e) { console.warn("[Radar] WS parse error", e); }
};
ws.onclose = () => {
_activeWs.onclose = (ev) => {
console.warn("[Radar] WebSocket closed (code=" + ev.code + " reason=" + ev.reason + ")");
State.browserWsConnected = false;
updateWsStatus("err");
toast(t("ws_reconnecting"), "warning");
if (wsReconnectTimer == null) toast(t("ws_reconnecting"), "warning");
scheduleReconnect();
};
ws.onerror = () => {
try { ws.close(); } catch (_) {}
_activeWs.onerror = (ev) => {
console.warn("[Radar] WebSocket error, will reconnect shortly");
try { if (_activeWs) _activeWs.close(); } catch (_) {}
};
}
@@ -841,10 +870,15 @@ document.addEventListener("DOMContentLoaded", () => {
applyI18n();
initUI();
refreshPanels();
/* Try to pre-load state via REST in case WS is slow */
fetch("/api/state").then(r => r.ok ? r.json() : null).then(data => {
if (data) onBackendFrame(data);
}).catch(() => {});
/* Pre-load state via REST immediately (before WS even connects) */
pollFallbackOnce();
/* Start safety-net REST poller every 2 s in addition to WebSocket */
startFallbackPoller();
connectWS();
loop();
console.info("%c[Radar Visualizer ready]",
"color:#89b4fa; font-weight:bold; padding:4px 8px; border-radius:6px; background:#181825;");
console.info(" - Open DevTools → Network → WS → /ws to see live messages");
console.info(" - If canvas is empty: check Console for errors, then check /api/state in browser");
});