From 0bbe22dd69bbb149e9fed74c87e7fc75e181483e Mon Sep 17 00:00:00 2001 From: James Barnett Date: Sat, 7 Apr 2018 23:22:58 +0100 Subject: Try to re-establish lost websocket connections --- public/javascripts/dash.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'public/javascripts/dash.js') diff --git a/public/javascripts/dash.js b/public/javascripts/dash.js index 7a8207c..e3f39a9 100644 --- a/public/javascripts/dash.js +++ b/public/javascripts/dash.js @@ -26,6 +26,7 @@ var dash = { var ws = new WebSocket(wsUri); ws.onopen = function () { console.log('Websocket connection established'); + $('#connection-error').hide(200); ws.send(JSON.stringify( { requestType: 'getCachedData', @@ -33,7 +34,14 @@ var dash = { } )); } - ws.onmessage = this.wsMessageHandler; + ws.onmessage = dash.wsMessageHandler; + + ws.onclose = function() { + // Usually caused by mobile devices going to sleep or the user minimising the browser app. + // The setTimeout will begin once the device wakes from sleep or the browser regains focus. + $('#connection-error').show(); + setTimeout(dash.initWsConnection, 2000); + } }, wsMessageHandler: function(messageEvent) { -- cgit v1.2.3