From b44cb3e0b20ba96470f568b7b09ed06a18e0199b Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sat, 14 Jul 2018 09:02:41 -0400 Subject: [PATCH] Add support for auto-reconnecting to websocket --- client.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/client.js b/client.js index 31ef3a1..7a4ec0b 100644 --- a/client.js +++ b/client.js @@ -37,6 +37,7 @@ const lowVoltage = 11.9; let stations = {}; let messages = []; +let aprsStream; if (Notification.permission !== "granted") { Notification.requestPermission(permission => { @@ -115,9 +116,7 @@ function alertVoltage(callsign) { `Voltage: ${stations[callsign].lastVoltage}`); } -let aprsStream = new WebSocket("wss://adamgoldsmith.name/APRSws"); -aprsStream.onmessage = function(event) { - let message = JSON.parse(event.data); +function handleMessage(message) { let callsign = `${message.from.call}-${message.from.ssid || 0}`; let date = new Date(); // TODO: could remove "message.recieved" from server @@ -150,7 +149,21 @@ aprsStream.onmessage = function(event) { redrawTable(); } -}; +} + +function connectToStream() { + aprsStream = new WebSocket("wss://adamgoldsmith.name/APRSws"); + aprsStream.onclose = () => { + // Try to reconnect every 5 seconds + let interval = window.setInterval(() => { + window.clearInterval(interval); + connectToStream(); + }, 5000); + }; + aprsStream.onmessage = event => handleMessage(JSON.parse(event.data)); +} + +connectToStream(); window.addEventListener("load", redrawTable); window.setInterval(redrawTable, 1000);