- Websocket ping interval 1 -> 5 seconds

- JS client websocket reconnect logic
This commit is contained in:
Fabio Manganiello 2018-02-07 22:14:38 +01:00
parent c47d910465
commit b675f631e9
2 changed files with 16 additions and 4 deletions

View file

@ -123,7 +123,7 @@ class HttpBackend(Backend):
try: try:
waiter = await websocket.ping() waiter = await websocket.ping()
await asyncio.wait_for(waiter, timeout=5) await asyncio.wait_for(waiter, timeout=5)
time.sleep(1) time.sleep(5)
except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e: except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e:
logging.info('Client {} closed connection'.format(websocket.remote_address[0])) logging.info('Client {} closed connection'.format(websocket.remote_address[0]))
self.active_websockets.remove(websocket) self.active_websockets.remove(websocket)

View file

@ -1,7 +1,8 @@
$(document).ready(function() { $(document).ready(function() {
var websocket; var websocket,
var dateTimeInterval; dateTimeInterval,
var eventListeners = []; websocketReconnectTimeout,
eventListeners = [];
var initWebsocket = function() { var initWebsocket = function() {
websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.websocket_port); websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.websocket_port);
@ -15,6 +16,17 @@ $(document).ready(function() {
listener(data); 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() { var initDateTime = function() {