From b675f631e9b0dda7a267ceb438d440335a5468a5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 7 Feb 2018 22:14:38 +0100 Subject: [PATCH] - Websocket ping interval 1 -> 5 seconds - JS client websocket reconnect logic --- platypush/backend/http/__init__.py | 2 +- .../backend/http/static/js/application.js | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 8637aa9fb..ac42d937f 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -123,7 +123,7 @@ class HttpBackend(Backend): try: waiter = await websocket.ping() await asyncio.wait_for(waiter, timeout=5) - time.sleep(1) + time.sleep(5) except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e: logging.info('Client {} closed connection'.format(websocket.remote_address[0])) self.active_websockets.remove(websocket) diff --git a/platypush/backend/http/static/js/application.js b/platypush/backend/http/static/js/application.js index 72db7af1d..2fd3084c3 100644 --- a/platypush/backend/http/static/js/application.js +++ b/platypush/backend/http/static/js/application.js @@ -1,7 +1,8 @@ $(document).ready(function() { - var websocket; - var dateTimeInterval; - var eventListeners = []; + var websocket, + dateTimeInterval, + websocketReconnectTimeout, + eventListeners = []; var initWebsocket = function() { websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.websocket_port); @@ -15,6 +16,17 @@ $(document).ready(function() { listener(data); } }; + + websocket.onerror = function(event) { + console.error(event); + }; + + websocket.onclose = function(event) { + console.log('Websocket closed, code: ' + event.code); + websocketReconnectTimeout = setTimeout(function() { + initWebsocket(); + }, 5000); + }; }; var initDateTime = function() {