From 8170a7baaee223aa5eb9facd200e99b8ed11f4ca Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 27 Jul 2015 18:51:45 -0700 Subject: [PATCH] WebSocketResource exposes socket.close() Allows for simplification of the keepalive construct. --- js/libtextsecure.js | 13 ++++++++----- libtextsecure/account_manager.js | 2 +- libtextsecure/keepalive.js | 5 ++--- libtextsecure/message_receiver.js | 2 +- libtextsecure/websocket-resources.js | 4 ++++ 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 4a1c648c..9eeed729 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38565,6 +38565,10 @@ TextSecureWebSocket = function (url, opts) { return new OutgoingWebSocketRequest(options, socket); }; + this.close = function() { + socket.close(3000); + }; + socket.onmessage = function(socketMessage) { var blob = socketMessage.data; var reader = new FileReader(); @@ -38621,10 +38625,9 @@ TextSecureWebSocket = function (url, opts) { * along with this program. If not, see . */ -function KeepAlive(websocketResource, socket) { +function KeepAlive(websocketResource) { if (websocketResource instanceof WebSocketResource) { this.wsr = websocketResource; - this.socket = socket; this.reset(); } else { throw new TypeError('KeepAlive expected a WebSocketResource'); @@ -38637,7 +38640,7 @@ KeepAlive.prototype = { clearTimeout(this.keepAliveTimer); clearTimeout(this.disconnectTimer); this.keepAliveTimer = setTimeout(function() { - this.disconnectTimer = setTimeout(this.socket.close, 10000); + this.disconnectTimer = setTimeout(this.wsr.close, 5000); this.wsr.sendRequest({ verb: 'GET', path: '/v1/keepalive', @@ -39393,7 +39396,7 @@ TextSecureServer = function () { console.log('Unknown websocket message', request.path); } }); - new KeepAlive(wsr, socket); + new KeepAlive(wsr); }); }).then(function() { return generateKeys(100, progressCallback); @@ -39541,7 +39544,7 @@ function generateKeys(count, progressCallback) { } this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.keepalive = new KeepAlive(this.wsr, this.socket); + this.keepalive = new KeepAlive(this.wsr); }, handleRequest: function(request) { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 7df05a5b..b07a17df 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, socket); + new KeepAlive(wsr); }); }).then(function() { return generateKeys(100, progressCallback); diff --git a/libtextsecure/keepalive.js b/libtextsecure/keepalive.js index 1dcfccbe..dba193db 100644 --- a/libtextsecure/keepalive.js +++ b/libtextsecure/keepalive.js @@ -14,10 +14,9 @@ * along with this program. If not, see . */ -function KeepAlive(websocketResource, socket) { +function KeepAlive(websocketResource) { if (websocketResource instanceof WebSocketResource) { this.wsr = websocketResource; - this.socket = socket; this.reset(); } else { throw new TypeError('KeepAlive expected a WebSocketResource'); @@ -30,7 +29,7 @@ KeepAlive.prototype = { clearTimeout(this.keepAliveTimer); clearTimeout(this.disconnectTimer); this.keepAliveTimer = setTimeout(function() { - this.disconnectTimer = setTimeout(this.socket.close, 10000); + this.disconnectTimer = setTimeout(this.wsr.close, 5000); this.wsr.sendRequest({ verb: 'GET', path: '/v1/keepalive', diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 4850603d..2e99b431 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -45,7 +45,7 @@ } this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.keepalive = new KeepAlive(this.wsr, this.socket); + this.keepalive = new KeepAlive(this.wsr); }, handleRequest: function(request) { diff --git a/libtextsecure/websocket-resources.js b/libtextsecure/websocket-resources.js index 265f4f2c..808a30e0 100644 --- a/libtextsecure/websocket-resources.js +++ b/libtextsecure/websocket-resources.js @@ -97,6 +97,10 @@ return new OutgoingWebSocketRequest(options, socket); }; + this.close = function() { + socket.close(3000); + }; + socket.onmessage = function(socketMessage) { var blob = socketMessage.data; var reader = new FileReader();