To keep websockets alive send every eight seconds a ping to the server.

This commit is contained in:
Sascha L. Teichmann 2015-03-13 12:29:55 +01:00
parent d2b6ff4fbb
commit dda0716053

View File

@ -15,9 +15,7 @@ L.Control.AutoUpdate = L.Control.extend({
return container; return container;
}, },
cbClick: function (e) { switchButtons: function() {
L.DomEvent.stopPropagation(e);
this.intendedFunction(this.pressed);
if (this.pressed) { if (this.pressed) {
this.pressed = false; this.pressed = false;
this.iconStart.setAttribute('class', 'fa fa-pause'); this.iconStart.setAttribute('class', 'fa fa-pause');
@ -29,18 +27,48 @@ L.Control.AutoUpdate = L.Control.extend({
} }
}, },
cbClick: function (e) {
L.DomEvent.stopPropagation(e);
this.intendedFunction(this.pressed);
this.switchButtons();
},
intendedFunction: function() { intendedFunction: function() {
alert('no function selected'); alert('no function selected');
}, },
stopUpdate: function() { stopUpdate: function() {
if (this.socket) {
this.socket.close(); this.socket.close();
this.socket = null;
}
}, },
autoUpdate: function() { autoUpdate: function() {
var me = this; var me = this;
this.socket = new WebSocket('ws://' + window.location.host + '/ws'); this.socket = new WebSocket('ws://' + window.location.host + '/ws');
this.socket.onerror = function(evt) {
me.stopUpdate();
me.switchButtons();
};
this.socket.onclose = this.socket.onerror;
this.socket.onopen = function(evt) {
// Sending pings every 5 secs to keep connection alive.
var heartbeat = function() {
if (heartbeat && me.socket) {
me.socket.send("PING");
setTimeout(heartbeat, 8000);
} else {
// Prevent sending pings to re-opened sockets.
heartbeat = null;
}
};
setTimeout(heartbeat, 8000);
};
this.socket.onmessage = function(evt) { this.socket.onmessage = function(evt) {
var updatePlayers = function(json) { var updatePlayers = function(json) {