ping server to keep connection open
This commit is contained in:
parent
afd9924a5d
commit
732f9ac089
1 changed files with 28 additions and 19 deletions
|
@ -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 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 socket = new WebSocket(URL);
|
||||||
|
|
||||||
|
var pingInterval;
|
||||||
|
|
||||||
//TODO: GUI
|
//TODO: GUI
|
||||||
socket.onerror = function(socketEvent) {
|
socket.onerror = function(socketEvent) {
|
||||||
console.log('Server is down :(');
|
console.log('Server is down :(');
|
||||||
|
clearInterval(pingInterval);
|
||||||
subscribeToPushMessageSemaphore++;
|
subscribeToPushMessageSemaphore++;
|
||||||
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
|
||||||
};
|
};
|
||||||
socket.onclose = function(socketEvent) {
|
socket.onclose = function(socketEvent) {
|
||||||
console.log('Server closed :(');
|
console.log('Server closed :(');
|
||||||
|
clearInterval(pingInterval);
|
||||||
subscribeToPushMessageSemaphore++;
|
subscribeToPushMessageSemaphore++;
|
||||||
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
setTimeout(function() { subscribeToPush(message_callback); }, 60000);
|
||||||
};
|
};
|
||||||
socket.onopen = function(socketEvent) {
|
socket.onopen = function(socketEvent) {
|
||||||
console.log('Connected to server!');
|
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) {
|
socket.onmessage = function(response) {
|
||||||
|
@ -904,24 +909,28 @@ function subscribeToPush(message_callback) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var proto;
|
if (message.type == 3) {
|
||||||
try {
|
console.log("Got pong message");
|
||||||
var plaintext = crypto.decryptWebsocketMessage(message.message);
|
} else if (message.type === undefined && message.id !== undefined) {
|
||||||
var proto = decodeIncomingPushMessageProtobuf(plaintext);
|
var proto;
|
||||||
// After this point, a) decoding errors are not the server's fault, and
|
try {
|
||||||
// b) we should handle them gracefully and tell the user they received an invalid message
|
var plaintext = crypto.decryptWebsocketMessage(message.message);
|
||||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
var proto = decodeIncomingPushMessageProtobuf(plaintext);
|
||||||
} catch (e) {
|
// After this point, a) decoding errors are not the server's fault, and
|
||||||
console.log("Error decoding message: " + e);
|
// b) we should handle them gracefully and tell the user they received an invalid message
|
||||||
return;
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||||
}
|
} catch (e) {
|
||||||
|
console.log("Error decoding message: " + e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
crypto.handleIncomingPushMessageProto(proto, function(decrypted) {
|
crypto.handleIncomingPushMessageProto(proto, function(decrypted) {
|
||||||
message_callback(decrypted);
|
message_callback(decrypted);
|
||||||
}); // Decrypts/decodes/fills in fields/etc
|
}); // Decrypts/decodes/fills in fields/etc
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//TODO: Tell the user decryption failed
|
//TODO: Tell the user decryption failed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue