diff --git a/Gruntfile.js b/Gruntfile.js index 6e4025ea..de38eccd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -53,7 +53,6 @@ module.exports = function(grunt) { 'libtextsecure/protobufs.js', 'libtextsecure/websocket.js', 'libtextsecure/websocket-resources.js', - 'libtextsecure/keepalive.js', 'libtextsecure/helpers.js', 'libtextsecure/stringview.js', 'libtextsecure/api.js', diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 2ad92fb2..e79a8529 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38563,7 +38563,7 @@ TextSecureWebSocket = function (url, opts) { ); }; - window.WebSocketResource = function(socket, handleRequest) { + window.WebSocketResource = function(socket, handleRequest, keepalive) { this.sendRequest = function(options) { return new OutgoingWebSocketRequest(options, socket); }; @@ -38609,63 +38609,60 @@ TextSecureWebSocket = function (url, opts) { }; reader.readAsArrayBuffer(blob); }; + + if (opts.keepalive) { + var keepalive = new KeepAlive(this, { + path : opts.keepalive.path, + disconnect : opts.keepalive.disconnect + }); + + this.resetKeepAliveTimer = keepalive.reset.bind(keepalive); + } + }; + + function KeepAlive(websocketResource, opts) { + if (websocketResource instanceof WebSocketResource) { + opts = opts || {}; + this.path = opts.path; + if (this.path === undefined) { + this.path = '/'; + } + this.disconnect = opts.disconnect; + if (this.disconnect === undefined) { + this.disconnect = false; + } + this.wsr = websocketResource; + this.reset(); + } else { + throw new TypeError('KeepAlive expected a WebSocketResource'); + } + } + + KeepAlive.prototype = { + constructor: KeepAlive, + reset: function() { + clearTimeout(this.keepAliveTimer); + clearTimeout(this.disconnectTimer); + this.keepAliveTimer = setTimeout(function() { + this.wsr.sendRequest({ + verb: 'GET', + path: this.path, + 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); + }, }; }()); -/* vim: ts=4:sw=4:expandtab - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -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 { - throw new TypeError('KeepAlive expected a WebSocketResource'); - } -} - -KeepAlive.prototype = { - constructor: KeepAlive, - reset: function() { - clearTimeout(this.keepAliveTimer); - clearTimeout(this.disconnectTimer); - this.keepAliveTimer = setTimeout(function() { - 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); - }, -}; - /* vim: ts=4:sw=4:expandtab * * This program is free software: you can redistribute it and/or modify @@ -39411,8 +39408,9 @@ TextSecureServer = function () { } else { console.log('Unknown websocket message', request.path); } + }, { + keepalive: { path: '/v1/keepalive' } }); - new KeepAlive(wsr, { disconnect: false }); }); }).then(function() { return generateKeys(100, progressCallback); @@ -39557,14 +39555,15 @@ function generateKeys(count, progressCallback) { eventTarget.dispatchEvent(ev); }); } - } + }; - this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.keepalive = new KeepAlive(this.wsr); + this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), { + keepalive: { path: '/v1/keepalive', disconnect: true } + }); }, handleRequest: function(request) { - this.keepalive.reset(); + this.wsr.resetKeepAliveTimer(); // TODO: handle different types of requests. for now we only expect // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 066be03a..e5a11413 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -72,8 +72,9 @@ } else { console.log('Unknown websocket message', request.path); } + }, { + keepalive: { path: '/v1/keepalive' } }); - new KeepAlive(wsr, { disconnect: false }); }); }).then(function() { return generateKeys(100, progressCallback); diff --git a/libtextsecure/keepalive.js b/libtextsecure/keepalive.js deleted file mode 100644 index ba50fca2..00000000 --- a/libtextsecure/keepalive.js +++ /dev/null @@ -1,52 +0,0 @@ -/* vim: ts=4:sw=4:expandtab - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -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 { - throw new TypeError('KeepAlive expected a WebSocketResource'); - } -} - -KeepAlive.prototype = { - constructor: KeepAlive, - reset: function() { - clearTimeout(this.keepAliveTimer); - clearTimeout(this.disconnectTimer); - this.keepAliveTimer = setTimeout(function() { - 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); - }, -}; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 2e99b431..ef2f251b 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -42,14 +42,15 @@ eventTarget.dispatchEvent(ev); }); } - } + }; - this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this)); - this.keepalive = new KeepAlive(this.wsr); + this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), { + keepalive: { path: '/v1/keepalive', disconnect: true } + }); }, handleRequest: function(request) { - this.keepalive.reset(); + this.wsr.resetKeepAliveTimer(); // TODO: handle different types of requests. for now we only expect // PUT /messages textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) { diff --git a/libtextsecure/websocket-resources.js b/libtextsecure/websocket-resources.js index 11b1ae64..4f0972ed 100644 --- a/libtextsecure/websocket-resources.js +++ b/libtextsecure/websocket-resources.js @@ -92,7 +92,7 @@ ); }; - window.WebSocketResource = function(socket, handleRequest) { + window.WebSocketResource = function(socket, handleRequest, keepalive) { this.sendRequest = function(options) { return new OutgoingWebSocketRequest(options, socket); }; @@ -138,6 +138,56 @@ }; reader.readAsArrayBuffer(blob); }; + + if (opts.keepalive) { + var keepalive = new KeepAlive(this, { + path : opts.keepalive.path, + disconnect : opts.keepalive.disconnect + }); + + this.resetKeepAliveTimer = keepalive.reset.bind(keepalive); + } + }; + + function KeepAlive(websocketResource, opts) { + if (websocketResource instanceof WebSocketResource) { + opts = opts || {}; + this.path = opts.path; + if (this.path === undefined) { + this.path = '/'; + } + this.disconnect = opts.disconnect; + if (this.disconnect === undefined) { + this.disconnect = false; + } + this.wsr = websocketResource; + this.reset(); + } else { + throw new TypeError('KeepAlive expected a WebSocketResource'); + } + } + + KeepAlive.prototype = { + constructor: KeepAlive, + reset: function() { + clearTimeout(this.keepAliveTimer); + clearTimeout(this.disconnectTimer); + this.keepAliveTimer = setTimeout(function() { + this.wsr.sendRequest({ + verb: 'GET', + path: this.path, + 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); + }, }; }());