From 838283f28b46fe7e04a7fba6b5d874c6f874abe7 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 22 Oct 2014 12:05:17 -0700 Subject: [PATCH] Send acks instead of closing and opening the socket Hopefully the real fix for #67 until we get protocol-level pings from the wc3 api. --- js/api.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/js/api.js b/js/api.js index 98503e7c..6ef51d1e 100644 --- a/js/api.js +++ b/js/api.js @@ -299,12 +299,12 @@ window.textsecure.api = function() { } else var params = $.param({}); - var reconnectTimer; + var keepAliveTimer; var reconnectSemaphore = 0; var socketWrapper = { onmessage: function() {}, ondisconnect: function() {}, onconnect: function() {} }; var connect = function() { - clearTimeout(reconnectTimer); + clearTimeout(keepAliveTimer); reconnectSemaphore++; if (reconnectSemaphore <= 0) return; @@ -312,12 +312,12 @@ window.textsecure.api = function() { if (socket) { socket.close(); } var socket = new WebSocket(URL+params); - function setReconnectTimeout() { - clearTimeout(reconnectTimer); - reconnectTimer = setTimeout(function() { - reconnectSemaphore--; - connect(); - }, 60000); + function resetKeepAliveTimer() { + clearTimeout(keepAliveTimer); + keepAliveTimer = setTimeout(function() { + socket.send(JSON.stringify({type: 1, id: 0})); + resetKeepAliveTimer(); + }, 50000); }; socket.onerror = function(socketEvent) { @@ -337,7 +337,7 @@ window.textsecure.api = function() { socket.onopen = function(socketEvent) { console.log('Connected to server!'); socketWrapper.onconnect(); - setReconnectTimeout(); + resetKeepAliveTimer(); }; //TODO: wrap onmessage so that we reconnect on missing pong @@ -356,7 +356,7 @@ window.textsecure.api = function() { console.log("Got invalid message from server: " + message); } - setReconnectTimeout(); + resetKeepAliveTimer(); }; socketWrapper.send = function(msg) {