reconnect on ws connection loss

This commit is contained in:
Sebastian Hugentobler 2023-02-23 13:15:57 +01:00
parent 459664a9a8
commit 90f8bb67b3
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0

View File

@ -14,10 +14,11 @@ let area;
let ws; let ws;
const uuid = self.crypto.randomUUID(); const uuid = self.crypto.randomUUID();
const wsUrl = "ws://localhost:3000/ws";
function setup() { function setup() {
area = document.querySelector(`#${areaId}`); area = document.querySelector(`#${areaId}`);
ws = new WebSocket("ws://localhost:3000/ws"); ws = new WebSocket(wsUrl);
setupUi(); setupUi();
setupWs(); setupWs();
@ -29,6 +30,12 @@ function setupUi() {
} }
function setupWs() { function setupWs() {
ws.onclose = function () {
setTimeout(() => {
ws = new WebSocket(wsUrl);
}, 2000);
};
ws.onmessage = function (e) { ws.onmessage = function (e) {
let payload = JSON.parse(e.data); let payload = JSON.parse(e.data);
if (payload.client !== uuid) { if (payload.client !== uuid) {
@ -37,7 +44,7 @@ function setupWs() {
}; };
} }
function onSelectionChange(event) { function onSelectionChange() {
const activeElement = document.activeElement; const activeElement = document.activeElement;
if (activeElement && activeElement.id === areaId) { if (activeElement && activeElement.id === areaId) {