From 7d2b41db11a0179475762a5a573956dfac959634 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 27 Jul 2015 20:17:00 -0700 Subject: [PATCH] Add auto-disconnect config to keepalive constructor Disconnects on no keepalive response by default. // FREEBIE --- js/libtextsecure.js | 20 +++++++++++++++----- libtextsecure/account_manager.js | 2 +- libtextsecure/keepalive.js | 18 ++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index aee402d6f..2ad92fb24 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38629,8 +38629,13 @@ TextSecureWebSocket = function (url, opts) { * along with this program. If not, see . */ -function KeepAlive(websocketResource) { +function KeepAlive(websocketResource, opts) { if (websocketResource instanceof WebSocketResource) { + opts = opts || {}; + this.disconnect = opts.disconnect; + if (this.disconnect === undefined) { + this.disconnect = true; + } this.wsr = websocketResource; this.reset(); } else { @@ -38644,14 +38649,19 @@ KeepAlive.prototype = { clearTimeout(this.keepAliveTimer); clearTimeout(this.disconnectTimer); this.keepAliveTimer = setTimeout(function() { - this.disconnectTimer = setTimeout(function() { - this.wsr.close(3001, 'No response to keepalive request'); - }.bind(this), 5000); this.wsr.sendRequest({ verb: 'GET', path: '/v1/keepalive', success: this.reset.bind(this) }); + if (this.disconnect) { + // automatically disconnect if server doesn't ack + this.disconnectTimer = setTimeout(function() { + this.wsr.close(3001, 'No response to keepalive request'); + }.bind(this), 1000); + } else { + this.reset(); + } }.bind(this), 55000); }, }; @@ -39402,7 +39412,7 @@ TextSecureServer = function () { console.log('Unknown websocket message', request.path); } }); - new KeepAlive(wsr); + new KeepAlive(wsr, { disconnect: false }); }); }).then(function() { return generateKeys(100, progressCallback); diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index b6bea115a..066be03a7 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -73,7 +73,7 @@ console.log('Unknown websocket message', request.path); } }); - new KeepAlive(wsr); + new KeepAlive(wsr, { disconnect: false }); }); }).then(function() { return generateKeys(100, progressCallback); diff --git a/libtextsecure/keepalive.js b/libtextsecure/keepalive.js index 039374c68..ba50fca2a 100644 --- a/libtextsecure/keepalive.js +++ b/libtextsecure/keepalive.js @@ -14,8 +14,13 @@ * along with this program. If not, see . */ -function KeepAlive(websocketResource) { +function KeepAlive(websocketResource, opts) { if (websocketResource instanceof WebSocketResource) { + opts = opts || {}; + this.disconnect = opts.disconnect; + if (this.disconnect === undefined) { + this.disconnect = true; + } this.wsr = websocketResource; this.reset(); } else { @@ -29,14 +34,19 @@ KeepAlive.prototype = { clearTimeout(this.keepAliveTimer); clearTimeout(this.disconnectTimer); this.keepAliveTimer = setTimeout(function() { - this.disconnectTimer = setTimeout(function() { - this.wsr.close(3001, 'No response to keepalive request'); - }.bind(this), 5000); this.wsr.sendRequest({ verb: 'GET', path: '/v1/keepalive', success: this.reset.bind(this) }); + if (this.disconnect) { + // automatically disconnect if server doesn't ack + this.disconnectTimer = setTimeout(function() { + this.wsr.close(3001, 'No response to keepalive request'); + }.bind(this), 1000); + } else { + this.reset(); + } }.bind(this), 55000); }, };