A more consistent a bug-free handling of websocket reconnection cycles

This commit is contained in:
Fabio Manganiello 2018-04-10 15:56:00 +02:00
parent 4efbcc50dd
commit 10bf54f961
2 changed files with 48 additions and 9 deletions

View file

@ -24,6 +24,7 @@ class HttpBackend(Backend):
http://localhost:8008/execute """ http://localhost:8008/execute """
websocket_ping_tries = 3 websocket_ping_tries = 3
websocket_ping_timeout = 10.0
def __init__(self, port=8008, websocket_port=8009, disable_websocket=False, def __init__(self, port=8008, websocket_port=8009, disable_websocket=False,
token=None, **kwargs): token=None, **kwargs):
@ -125,8 +126,8 @@ class HttpBackend(Backend):
while True: while True:
try: try:
waiter = await websocket.ping() waiter = await websocket.ping()
await asyncio.wait_for(waiter, timeout=5) await asyncio.wait_for(waiter, timeout=self.websocket_ping_timeout)
time.sleep(5) time.sleep(self.websocket_ping_timeout)
except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e: except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed) as e:
close = False close = False
if isinstance(e, asyncio.TimeoutError): if isinstance(e, asyncio.TimeoutError):

View file

@ -1,11 +1,34 @@
$(document).ready(function() { $(document).ready(function() {
var websocket, var websocket,
pendingConnection = false,
openedWebsocket,
dateTimeInterval, dateTimeInterval,
websocketReconnectInterval, websocketTimeoutId,
websocketReconnectMsecs = 4000,
eventListeners = []; eventListeners = [];
var initWebsocket = function() { var initWebsocket = function() {
websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.websocket_port); try {
websocket = new WebSocket('ws://' + window.location.hostname + ':' + window.websocket_port);
} catch (err) {
websocket = undefined;
return;
}
pendingConnection = websocket;
var onWebsocketTimeout = function(sock) {
return function() {
console.log('Websocket reconnection timed out, retrying');
pendingConnection = false;
sock.close();
sock.onclose({ code: 1000 });
};
};
websocketTimeoutId = setTimeout(
onWebsocketTimeout(websocket), websocketReconnectMsecs);
websocket.onmessage = function(event) { websocket.onmessage = function(event) {
for (var listener of eventListeners) { for (var listener of eventListeners) {
data = event.data; data = event.data;
@ -18,10 +41,22 @@ $(document).ready(function() {
}; };
websocket.onopen = function(event) { websocket.onopen = function(event) {
if (openedWebsocket) {
console.log("There's already an opened websocket connection, closing the newly opened one");
this.onclose = function() {};
this.close();
}
console.log('Websocket connection successful'); console.log('Websocket connection successful');
if (websocketReconnectInterval) { openedWebsocket = this;
clearInterval(websocketReconnectInterval);
websocketReconnectInterval = undefined; if (pendingConnection) {
pendingConnection = false;
}
if (websocketTimeoutId) {
clearInterval(websocketTimeoutId);
websocketTimeoutId = undefined;
} }
}; };
@ -31,9 +66,12 @@ $(document).ready(function() {
websocket.onclose = function(event) { websocket.onclose = function(event) {
console.log('Websocket closed, code: ' + event.code); console.log('Websocket closed, code: ' + event.code);
websocketReconnectInterval = setInterval(function() { openedWebsocket = undefined;
if (!pendingConnection) {
pendingConnection = true;
initWebsocket(); initWebsocket();
}, 3000); }
}; };
}; };