From 1f8856fa6962e851a3adf64ee81ec00665f54c04 Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 23 Jul 2015 15:41:17 -0700 Subject: [PATCH] MessageReceiver accepts a server websocket url // FREEBIE --- Gruntfile.js | 2 +- js/background.js | 2 +- js/libtextsecure.js | 34 +++++++++++++------------------ libtextsecure/api.js | 22 ++++++++------------ libtextsecure/message_receiver.js | 12 +++++------ 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 973f256e..de38eccd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -124,7 +124,7 @@ module.exports = function(grunt) { files: [{ expand: true, dest: 'dist/', src: ['<%= dist.src %>'] }], options: { process: function(content, srcpath) { - if (srcpath.match('libtextsecure')) { + if (srcpath.match('libtextsecure') || srcpath.match('background.js')) { return content.replace( /textsecure-service-staging.whispersystems.org/g, 'textsecure-service-ca.whispersystems.org:4433').replace( diff --git a/js/background.js b/js/background.js index ec506667..1c9f6088 100644 --- a/js/background.js +++ b/js/background.js @@ -60,7 +60,7 @@ if (!textsecure.registration.isDone()) { return; } // initialize the socket and start listening for messages - messageReceiver = new textsecure.MessageReceiver(window); + messageReceiver = new textsecure.MessageReceiver('wss://textsecure-service-staging.whispersystems.org', window); } function onContactReceived(ev) { diff --git a/js/libtextsecure.js b/js/libtextsecure.js index c3ed5ac4..a47b0dfd 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -39256,23 +39256,17 @@ TextSecureServer = function () { }); }; - var getWebsocket = function(url, auth, reconnectTimeout) { - var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?'; - var params = ''; - if (auth) { - var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); - var password = textsecure.storage.get("password"); - var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); - } - return TextSecureWebSocket(URL+params) - } - - self.getMessageWebsocket = function() { - return getWebsocket(URL_CALLS['push'], true, 1000); + self.getMessageWebsocket = function(url) { + var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); + var password = textsecure.storage.get("password"); + var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); + var url = url + URL_CALLS['push'] + '/?' + params; + return TextSecureWebSocket(url) } self.getTempWebsocket = function() { - return getWebsocket(URL_CALLS['temp_push'], false, 1000); + var url = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['temp_push'] + '/?'; + return TextSecureWebSocket(url); } return self; @@ -39473,21 +39467,21 @@ function generateKeys(count, progressCallback) { 'use strict'; window.textsecure = window.textsecure || {}; - function MessageReceiver(eventTarget) { + function MessageReceiver(url, eventTarget) { if (eventTarget instanceof EventTarget) { this.target = eventTarget; } else { throw new TypeError('MessageReceiver expected an EventTarget'); } - this.connect(); + this.connect(url); } MessageReceiver.prototype = { constructor: MessageReceiver, - connect: function() { + connect: function(url) { var eventTarget = this.target; // initialize the socket and start listening for messages - this.socket = TextSecureServer.getMessageWebsocket(); + this.socket = TextSecureServer.getMessageWebsocket(url); this.socket.onclose = function(e) { if (e.code === 1006) { // possible 403. Make an request to confirm @@ -39689,8 +39683,8 @@ function generateKeys(count, progressCallback) { } }; - textsecure.MessageReceiver = function (eventTarget) { - var messageReceiver = new MessageReceiver(eventTarget); + textsecure.MessageReceiver = function (url, eventTarget) { + var messageReceiver = new MessageReceiver(url, eventTarget); this.getStatus = function() { return messageReceiver.getStatus(); } diff --git a/libtextsecure/api.js b/libtextsecure/api.js index 7aa25c3f..5b6c00b6 100644 --- a/libtextsecure/api.js +++ b/libtextsecure/api.js @@ -319,23 +319,17 @@ TextSecureServer = function () { }); }; - var getWebsocket = function(url, auth, reconnectTimeout) { - var URL = URL_BASE.replace(/^http/g, 'ws') + url + '/?'; - var params = ''; - if (auth) { - var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); - var password = textsecure.storage.get("password"); - var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); - } - return TextSecureWebSocket(URL+params) - } - - self.getMessageWebsocket = function() { - return getWebsocket(URL_CALLS['push'], true, 1000); + self.getMessageWebsocket = function(url) { + var user = textsecure.storage.user.getNumber() + "." + textsecure.storage.user.getDeviceId(); + var password = textsecure.storage.get("password"); + var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password); + var url = url + URL_CALLS['push'] + '/?' + params; + return TextSecureWebSocket(url) } self.getTempWebsocket = function() { - return getWebsocket(URL_CALLS['temp_push'], false, 1000); + var url = URL_BASE.replace(/^http/g, 'ws') + URL_CALLS['temp_push'] + '/?'; + return TextSecureWebSocket(url); } return self; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 3dc2e8cd..aa400c4d 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -18,21 +18,21 @@ 'use strict'; window.textsecure = window.textsecure || {}; - function MessageReceiver(eventTarget) { + function MessageReceiver(url, eventTarget) { if (eventTarget instanceof EventTarget) { this.target = eventTarget; } else { throw new TypeError('MessageReceiver expected an EventTarget'); } - this.connect(); + this.connect(url); } MessageReceiver.prototype = { constructor: MessageReceiver, - connect: function() { + connect: function(url) { var eventTarget = this.target; // initialize the socket and start listening for messages - this.socket = TextSecureServer.getMessageWebsocket(); + this.socket = TextSecureServer.getMessageWebsocket(url); this.socket.onclose = function(e) { if (e.code === 1006) { // possible 403. Make an request to confirm @@ -234,8 +234,8 @@ } }; - textsecure.MessageReceiver = function (eventTarget) { - var messageReceiver = new MessageReceiver(eventTarget); + textsecure.MessageReceiver = function (url, eventTarget) { + var messageReceiver = new MessageReceiver(url, eventTarget); this.getStatus = function() { return messageReceiver.getStatus(); }