diff --git a/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt b/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt index fbb2dfa..5447817 100644 --- a/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt +++ b/src/main/kotlin/de/pheerai/buzzer/clients/gameMasterClient.kt @@ -21,6 +21,7 @@ fun HTML.createGameMasterDocument() { src = "https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer = true } + script { src = "https://unpkg.com/robust-websocket@1.0.0/robust-websocket.js" } script { src = "/assets/buzzerWebSocket.js" } } body { diff --git a/src/main/kotlin/de/pheerai/buzzer/clients/playerClient.kt b/src/main/kotlin/de/pheerai/buzzer/clients/playerClient.kt index ea3bc3c..483832e 100644 --- a/src/main/kotlin/de/pheerai/buzzer/clients/playerClient.kt +++ b/src/main/kotlin/de/pheerai/buzzer/clients/playerClient.kt @@ -21,6 +21,7 @@ fun HTML.createPlayerDocument() { src = "https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer = true } + script { src = "https://unpkg.com/robust-websocket@1.0.0/robust-websocket.js" } script { src = "/assets/buzzerWebSocket.js" } diff --git a/src/main/resources/js/buzzerWebSocket.js b/src/main/resources/js/buzzerWebSocket.js index 82d9c1b..f59f0b2 100644 --- a/src/main/resources/js/buzzerWebSocket.js +++ b/src/main/resources/js/buzzerWebSocket.js @@ -2,33 +2,7 @@ function startWebSocket(socketPath, messageHandler) { const protocolPrefix = (window.location.protocol === 'https:') ? 'wss:' : 'ws:'; - const webSocket = new WebSocket(`${protocolPrefix}//${location.host}${socketPath}`); - webSocket.onerror = (ev) => { - switch (ev.code) { - case 1000: - console.log("[WS]: Closed normally") - break - default: - console.log("[WS]: Closed abnormally") - reconnectWebSocket(socketPath, messageHandler); - } - }; - webSocket.onclose = (ev) => { - switch (ev.code) { - case 'ECONNREFUSED': - reconnectWebSocket(socketPath, messageHandler); - break - default: - webSocket.onerror(ev); - } - }; + const webSocket = new RobustWebSocket(`${protocolPrefix}//${location.host}${socketPath}`); webSocket.onmessage = messageHandler ? messageHandler : () => {}; Alpine.data.buzzerWebSocket = webSocket } - -function reconnectWebSocket(socketPath, messageHandler) { - setTimeout(() => { - console.log("[WS]: Reconnecting...") - startWebSocket(socketPath, messageHandler) - }, 2000) -}