ws reconnect

main
Bel LaPointe 2025-10-15 09:34:58 -06:00
parent eecb67bbaa
commit 75d330088e
1 changed files with 19 additions and 7 deletions

View File

@ -57,15 +57,14 @@
} }
} }
ws = new WebSocket(`${window.location.protocol == "https:" ? "wss" : "ws"}://${window.location.host}/ws${window.location.search}`); function ws_onopen(evt) {
ws.onopen = function(evt) { print("CONNECTED");
print("READY");
} }
ws.onclose = function(evt) { function ws_onclose(evt) {
print("CLOSE"); print("CLOSED");
ws = null; ws = null;
} }
ws.onmessage = function(evt) { function ws_onmessage(evt) {
console.log("evt.data:", evt.data) console.log("evt.data:", evt.data)
const data = JSON.parse(evt.data); const data = JSON.parse(evt.data);
@ -89,9 +88,22 @@
print(data.QuietText); print(data.QuietText);
} }
} }
ws.onerror = function(evt) { function ws_onerror(evt) {
print("ERROR: " + evt.data); print("ERROR: " + evt.data);
} }
function ws_ensure_connected() {
if (ws) {
return;
}
print("CONNECTING...");
ws = new WebSocket(`${window.location.protocol == "https:" ? "wss" : "ws"}://${window.location.host}/ws${window.location.search}`);
ws.onerror = ws_onerror;
ws.onmessage = ws_onmessage;
ws.onclose = ws_onclose;
ws.onopen = ws_onopen;
}
setInterval(ws_ensure_connected, 1000);
ws_ensure_connected();
document.getElementById("send").onclick = function(evt) { document.getElementById("send").onclick = function(evt) {
if (!ws || !input.value) { if (!ws || !input.value) {