mirror of
https://github.com/ad1217/PrinterStatus
synced 2024-11-21 15:03:48 -05:00
Automatically reconnect to server websocket from frontend
This commit is contained in:
parent
0eedd689b8
commit
ca74c210b5
14
src/App.vue
14
src/App.vue
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user