From ca74c210b50bdb49234d66f7a58669a190bc7734 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 9 Nov 2020 12:45:24 -0500 Subject: [PATCH] Automatically reconnect to server websocket from frontend --- src/App.vue | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/App.vue b/src/App.vue index a41df54..8b935ba 100644 --- a/src/App.vue +++ b/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); + }); } }