2
0
mirror of https://github.com/ad1217/PrinterStatus synced 2024-11-21 23:13:49 -05:00

Automatically reconnect to server websocket from frontend

This commit is contained in:
Adam Goldsmith 2020-11-09 12:45:24 -05:00
parent 0eedd689b8
commit ca74c210b5

View File

@ -25,11 +25,15 @@ export default class App extends Vue {
} = {};
mounted() {
this.connectWebsocket();
}
connectWebsocket() {
let loc = window.location;
const ws_uri: string =
(loc.protocol === 'https:' ? 'wss://' : 'ws://') + loc.host + '/ws';
this.websocket = new WebSocket(ws_uri);
this.websocket.onmessage = (ev: MessageEvent) => {
this.websocket.addEventListener('message', (ev: MessageEvent) => {
const event: messages.ExtendedMessage = JSON.parse(ev.data as string);
console.log(event);
@ -39,8 +43,14 @@ export default class App extends Vue {
this.$set(this.printers, event.printer, event.current);
} else if ('history' in event) {
this.$set(this.printers, event.printer, event.history);
} else if ('remote_ws_status' in event) {
}
};
});
this.websocket.addEventListener('close', () => {
console.log('Lost connection to server reconnecting in 5 sec');
window.setTimeout(this.connectWebsocket, 5000);
});
}
}
</script>