Keep trying to connect in case the websocket goes down

This commit is contained in:
Fabio Manganiello 2018-04-09 23:14:59 +02:00
parent 75e958bb38
commit d380e0f398

View file

@ -1,7 +1,7 @@
$(document).ready(function() { $(document).ready(function() {
var websocket, var websocket,
dateTimeInterval, dateTimeInterval,
websocketReconnectTimeout, websocketReconnectInterval,
eventListeners = []; eventListeners = [];
var initWebsocket = function() { var initWebsocket = function() {
@ -19,6 +19,10 @@ $(document).ready(function() {
websocket.onopen = function(event) { websocket.onopen = function(event) {
console.log('Websocket connection successful'); console.log('Websocket connection successful');
if (websocketReconnectInterval) {
clearInterval(websocketReconnectInterval);
websocketReconnectInterval = undefined;
}
}; };
websocket.onerror = function(event) { websocket.onerror = function(event) {
@ -27,7 +31,7 @@ $(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);
websocketReconnectTimeout = setTimeout(function() { websocketReconnectInterval = setInterval(function() {
initWebsocket(); initWebsocket();
}, 5000); }, 5000);
}; };