Add textsecure.MessageReceiver

Encapsulate the websocket resources and socket setup process in a
friendly OO class. The MessageReceiver constructor expects an instance
of EventTarget on which to fire message events asynchronously. The
provider of the EventTarget can then add/remove listeners as desired.
This commit is contained in:
lilia 2015-04-29 11:56:40 -07:00
parent bf2bf4cfd9
commit 36b1e87214
5 changed files with 220 additions and 27 deletions

View file

@ -16,7 +16,7 @@
;(function() {
'use strict';
var socket;
var messageReceiver;
if (!localStorage.getItem('first_install_ran')) {
localStorage.setItem('first_install_ran', 1);
@ -29,10 +29,10 @@
extension.on('registration_done', init);
window.getSocketStatus = function() {
if (socket) {
return socket.getStatus();
if (messageReceiver) {
return messageReceiver.getStatus();
} else {
return WebSocket.CONNECTING;
return -1;
}
};
@ -40,30 +40,16 @@
if (!textsecure.registration.isDone()) { return; }
// initialize the socket and start listening for messages
socket = textsecure.api.getMessageWebsocket();
new WebSocketResource(socket, function(request) {
// TODO: handle different types of requests. for now we only expect
// PUT /messages <encrypted IncomingPushMessageSignal>
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
// After this point, decoding errors are not the server's
// fault, and we should handle them gracefully and tell the
// user they received an invalid message
request.respond(200, 'OK');
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
onDeliveryReceipt(proto);
} else {
onMessageReceived(proto);
}
}).catch(function(e) {
console.log("Error handling incoming message:", e);
extension.trigger('error', e);
request.respond(500, 'Bad encrypted websocket message');
});
messageReceiver = new textsecure.MessageReceiver(window);
window.addEventListener('signal', function(ev) {
var proto = ev.proto;
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
onDeliveryReceipt(proto);
} else {
onMessageReceived(proto);
}
});
messageReceiver.connect();
// refresh views
var views = extension.windows.getViews();