ping server to keep connection open

This commit is contained in:
Matt Corallo 2014-04-04 04:47:04 -04:00
parent afd9924a5d
commit 732f9ac089

View file

@ -881,19 +881,24 @@ function subscribeToPush(message_callback) {
var URL = URL_BASE.replace(/^http:/g, "ws:").replace(/^https:/g, "wss:") + URL_CALLS['push'] + "/?user=%2B" + getString(user).substring(1) + "&password=" + getString(password);
var socket = new WebSocket(URL);
var pingInterval;
//TODO: GUI
socket.onerror = function(socketEvent) {
console.log('Server is down :(');
clearInterval(pingInterval);
subscribeToPushMessageSemaphore++;
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
};
socket.onclose = function(socketEvent) {
console.log('Server closed :(');
clearInterval(pingInterval);
subscribeToPushMessageSemaphore++;
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
};
socket.onopen = function(socketEvent) {
console.log('Connected to server!');
pingInterval = setInterval(function() { console.log("Sending server ping message."); socket.send(JSON.stringify({type: 2})); }, 30000);
};
socket.onmessage = function(response) {
@ -904,6 +909,9 @@ function subscribeToPush(message_callback) {
return;
}
if (message.type == 3) {
console.log("Got pong message");
} else if (message.type === undefined && message.id !== undefined) {
var proto;
try {
var plaintext = crypto.decryptWebsocketMessage(message.message);
@ -923,6 +931,7 @@ function subscribeToPush(message_callback) {
} catch (e) {
//TODO: Tell the user decryption failed
}
}
};
}