From 732f9ac08915c31e4b0357e774ff023f7ce1daca Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 4 Apr 2014 04:47:04 -0400 Subject: [PATCH] ping server to keep connection open --- js/helpers.js | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/js/helpers.js b/js/helpers.js index 2f4ecd8f7..0dd32c36f 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -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,24 +909,28 @@ function subscribeToPush(message_callback) { return; } - var proto; - try { - var plaintext = crypto.decryptWebsocketMessage(message.message); - var proto = decodeIncomingPushMessageProtobuf(plaintext); - // After this point, a) decoding errors are not the server's fault, and - // b) we should handle them gracefully and tell the user they received an invalid message - socket.send(JSON.stringify({type: 1, id: message.id})); - } catch (e) { - console.log("Error decoding message: " + e); - 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); + var proto = decodeIncomingPushMessageProtobuf(plaintext); + // After this point, a) decoding errors are not the server's fault, and + // b) we should handle them gracefully and tell the user they received an invalid message + socket.send(JSON.stringify({type: 1, id: message.id})); + } catch (e) { + console.log("Error decoding message: " + e); + return; + } - try { - crypto.handleIncomingPushMessageProto(proto, function(decrypted) { - message_callback(decrypted); - }); // Decrypts/decodes/fills in fields/etc - } catch (e) { - //TODO: Tell the user decryption failed + try { + crypto.handleIncomingPushMessageProto(proto, function(decrypted) { + message_callback(decrypted); + }); // Decrypts/decodes/fills in fields/etc + } catch (e) { + //TODO: Tell the user decryption failed + } } }; }