From 90f8bb67b38c81001d0bc789473b5cc77b446c69 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Thu, 23 Feb 2023 13:15:57 +0100 Subject: [PATCH] reconnect on ws connection loss --- assets/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/assets/index.js b/assets/index.js index acd4d01..5c89024 100644 --- a/assets/index.js +++ b/assets/index.js @@ -14,10 +14,11 @@ let area; let ws; const uuid = self.crypto.randomUUID(); +const wsUrl = "ws://localhost:3000/ws"; function setup() { area = document.querySelector(`#${areaId}`); - ws = new WebSocket("ws://localhost:3000/ws"); + ws = new WebSocket(wsUrl); setupUi(); setupWs(); @@ -29,6 +30,12 @@ function setupUi() { } function setupWs() { + ws.onclose = function () { + setTimeout(() => { + ws = new WebSocket(wsUrl); + }, 2000); + }; + ws.onmessage = function (e) { let payload = JSON.parse(e.data); if (payload.client !== uuid) { @@ -37,7 +44,7 @@ function setupWs() { }; } -function onSelectionChange(event) { +function onSelectionChange() { const activeElement = document.activeElement; if (activeElement && activeElement.id === areaId) {