adjust websocket code

This commit is contained in:
Iris Lightshard 2023-07-12 01:34:50 -06:00
parent eba9792703
commit 28ac847ff7
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -163,12 +163,12 @@ function dial() {
if (conn) { if (conn) {
conn.close(1000); conn.close(1000);
} }
conn = new WebSocket(`ws://${location.host}/subscribe`, `${tableKey.name}.${tableKey.passcode}`); const wsProto = location.protocol == "https:" ? "wss" : "ws";
conn = new WebSocket(`${wsProto}://${location.host}/subscribe`, `${tableKey.name}.${tableKey.passcode}`);
conn.addEventListener("close", e => { conn.addEventListener("close", e => {
if (e.code == 1006) { if (e.code == 1006 && e.wasClean) {
setErr("Table not found - check the name and passcode are correct"); setErr("Table not found - check the name and passcode are correct");
} else if (e.code > 1001) { } else if (e.code > 1001) {
// TODO: add message to let user know they are reconnecting
setErr("Websocket error: trying to reconnect"); setErr("Websocket error: trying to reconnect");
setTimeout(dial, 1000) setTimeout(dial, 1000)
} else { } else {
@ -186,7 +186,7 @@ function dial() {
} }
}); });
conn.addEventListener("open", e => { conn.addEventListener("open", e => {
// TODO: add message to let user know they are at the table closeErr();
tabletop = document.getElementById("tabletop"); tabletop = document.getElementById("tabletop");
if (tabletop) { if (tabletop) {
tabletop.style.display = "block"; tabletop.style.display = "block";