Add support for auto-reconnecting to websocket
This commit is contained in:
parent
6cbacfe4d8
commit
13c8dfea42
21
client.js
21
client.js
@ -35,6 +35,7 @@ const lowVoltage = 11.9;
|
|||||||
|
|
||||||
let stations = {};
|
let stations = {};
|
||||||
let messages = [];
|
let messages = [];
|
||||||
|
let aprsStream;
|
||||||
|
|
||||||
if (Notification.permission !== "granted") {
|
if (Notification.permission !== "granted") {
|
||||||
Notification.requestPermission(permission => {
|
Notification.requestPermission(permission => {
|
||||||
@ -113,9 +114,7 @@ function alertVoltage(callsign) {
|
|||||||
`Voltage: ${stations[callsign].lastVoltage}`);
|
`Voltage: ${stations[callsign].lastVoltage}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let aprsStream = new WebSocket("wss://adamgoldsmith.name/APRSws");
|
function handleMessage(message) {
|
||||||
aprsStream.onmessage = function(event) {
|
|
||||||
let message = JSON.parse(event.data);
|
|
||||||
let callsign = `${message.from.call}-${message.from.ssid || 0}`;
|
let callsign = `${message.from.call}-${message.from.ssid || 0}`;
|
||||||
let date = new Date(); // TODO: could remove "message.recieved" from server
|
let date = new Date(); // TODO: could remove "message.recieved" from server
|
||||||
|
|
||||||
@ -148,7 +147,21 @@ aprsStream.onmessage = function(event) {
|
|||||||
|
|
||||||
redrawTable();
|
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.addEventListener("load", redrawTable);
|
||||||
window.setInterval(redrawTable, 1000);
|
window.setInterval(redrawTable, 1000);
|
||||||
|
Loading…
Reference in New Issue
Block a user