Fix reconnect bugs from previous commit

Forgot to bind the socket event handler, and the then() handler should
come before the catch() handler or else it will execute every time the
catch handler executes.

// FREEBIE
This commit is contained in:
lilia 2015-09-09 01:45:15 -07:00
parent 16de5aa3d3
commit 6b483195cb
2 changed files with 24 additions and 18 deletions

View file

@ -39383,28 +39383,31 @@ function generateKeys(count, progressCallback) {
window.textsecure = window.textsecure || {};
function MessageReceiver(url, eventTarget) {
this.url = url;
if (eventTarget instanceof EventTarget) {
this.target = eventTarget;
} else {
throw new TypeError('MessageReceiver expected an EventTarget');
}
this.connect(url);
this.connect();
}
MessageReceiver.prototype = {
constructor: MessageReceiver,
connect: function(url) {
connect: function() {
var eventTarget = this.target;
// initialize the socket and start listening for messages
this.socket = TextSecureServer.getMessageWebsocket(url);
this.socket = TextSecureServer.getMessageWebsocket(this.url);
this.socket.onclose = function(e) {
// possible 403. Make a request to confirm
TextSecureServer.getDevices(textsecure.storage.user.getNumber()).catch(function(e) {
var ev = new Event('textsecure:error');
ev.error = e;
eventTarget.dispatchEvent(ev);
}).then(this.connect.bind(this)); // No HTTP error? Reconnect.
};
TextSecureServer.getDevices(textsecure.storage.user.getNumber()).
then(this.connect.bind(this)).
catch(function(e) {
var ev = new Event('textsecure:error');
ev.error = e;
eventTarget.dispatchEvent(ev);
});
}.bind(this);
this.wsr = new WebSocketResource(this.socket, {
handleRequest: this.handleRequest.bind(this),

View file

@ -7,28 +7,31 @@
window.textsecure = window.textsecure || {};
function MessageReceiver(url, eventTarget) {
this.url = url;
if (eventTarget instanceof EventTarget) {
this.target = eventTarget;
} else {
throw new TypeError('MessageReceiver expected an EventTarget');
}
this.connect(url);
this.connect();
}
MessageReceiver.prototype = {
constructor: MessageReceiver,
connect: function(url) {
connect: function() {
var eventTarget = this.target;
// initialize the socket and start listening for messages
this.socket = TextSecureServer.getMessageWebsocket(url);
this.socket = TextSecureServer.getMessageWebsocket(this.url);
this.socket.onclose = function(e) {
// possible 403. Make a request to confirm
TextSecureServer.getDevices(textsecure.storage.user.getNumber()).catch(function(e) {
var ev = new Event('textsecure:error');
ev.error = e;
eventTarget.dispatchEvent(ev);
}).then(this.connect.bind(this)); // No HTTP error? Reconnect.
};
TextSecureServer.getDevices(textsecure.storage.user.getNumber()).
then(this.connect.bind(this)).
catch(function(e) {
var ev = new Event('textsecure:error');
ev.error = e;
eventTarget.dispatchEvent(ev);
});
}.bind(this);
this.wsr = new WebSocketResource(this.socket, {
handleRequest: this.handleRequest.bind(this),