New websocket protocol
This commit is contained in:
parent
9364cee578
commit
7f04439b37
6 changed files with 87 additions and 24 deletions
32
js/api.js
32
js/api.js
|
@ -356,19 +356,27 @@ window.textsecure.api = function () {
|
|||
|
||||
//TODO: wrap onmessage so that we reconnect on missing pong
|
||||
socket.onmessage = function(response) {
|
||||
try {
|
||||
var message = JSON.parse(response.data);
|
||||
} catch (e) {
|
||||
console.log('Error parsing server JSON message: ' + response);
|
||||
return;
|
||||
}
|
||||
var blob = response.data;
|
||||
var reader = new FileReader();
|
||||
reader.addEventListener("loadend", function() {
|
||||
// reader.result contains the contents of blob as a typed array
|
||||
try {
|
||||
var message = textsecure.protobuf.WebSocketMessage.decode(reader.result);
|
||||
console.log(message);
|
||||
if (message.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST ) {
|
||||
socketWrapper.onmessage(message.request);
|
||||
}
|
||||
else {
|
||||
throw "Got invalid message from server: " + message;
|
||||
}
|
||||
|
||||
if ((message.type === undefined && message.id !== undefined) || message.type === 4) {
|
||||
socketWrapper.onmessage(message);
|
||||
}
|
||||
else {
|
||||
console.log("Got invalid message from server: " + message);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error parsing server JSON message: ' + response);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
reader.readAsArrayBuffer(blob);
|
||||
|
||||
resetKeepAliveTimer();
|
||||
};
|
||||
|
|
|
@ -24,13 +24,12 @@
|
|||
} else {
|
||||
if (textsecure.registration.isDone()) {
|
||||
textsecure.subscribeToPush(function(message) {
|
||||
Whisper.Threads.addIncomingMessage(message).then(function() {
|
||||
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||
': "' + getString(message.message.body) + '"');
|
||||
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
||||
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||
extension.navigator.setBadgeText(newUnreadCount);
|
||||
});
|
||||
Whisper.Threads.addIncomingMessage(message);
|
||||
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||
': "' + getString(message.message.body) + '"');
|
||||
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
||||
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||
extension.navigator.setBadgeText(newUnreadCount);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -241,12 +241,19 @@ window.textsecure.subscribeToPush = function(message_callback) {
|
|||
var socket = textsecure.api.getMessageWebsocket();
|
||||
|
||||
socket.onmessage = function(message) {
|
||||
textsecure.protocol.decryptWebsocketMessage(message.message).then(function(plaintext) {
|
||||
textsecure.protocol.decryptWebsocketMessage(message.body).then(function(plaintext) {
|
||||
var proto = textsecure.protobuf.IncomingPushMessageSignal.decode(plaintext);
|
||||
// After this point, a) decoding errors are not the server's fault, and
|
||||
// b) we should handle them gracefully and tell the user they received an invalid message
|
||||
console.log("Successfully decoded message with id: " + message.id);
|
||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||
|
||||
socket.send(
|
||||
new textsecure.protobuf.WebSocketMessage({
|
||||
response: { id: message.id, message: 'OK', status: 200 },
|
||||
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE
|
||||
}).encode().toArrayBuffer()
|
||||
);
|
||||
|
||||
return textsecure.protocol.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
||||
// Delivery receipt
|
||||
if (decrypted === null)
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
},
|
||||
|
||||
sendMessage: function(message, attachments) {
|
||||
encodeAttachments(attachments).then(function(base64_attachments) {
|
||||
return encodeAttachments(attachments).then(function(base64_attachments) {
|
||||
var timestamp = Date.now();
|
||||
this.messages().add({ type: 'outgoing',
|
||||
body: message,
|
||||
|
@ -155,7 +155,7 @@
|
|||
|
||||
addIncomingMessage: function(decrypted) {
|
||||
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
||||
thread.receiveMessage(decrypted);
|
||||
return thread.receiveMessage(decrypted);
|
||||
}
|
||||
}))();
|
||||
})();
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
var pushMessages = loadProtoBufs('IncomingPushMessageSignal.proto');
|
||||
var protocolMessages = loadProtoBufs('WhisperTextProtocol.proto');
|
||||
var subProtocolMessages = loadProtoBufs('SubProtocol.proto');
|
||||
var deviceMessages = loadProtoBufs('DeviceMessages.proto');
|
||||
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
@ -16,6 +17,9 @@
|
|||
PreKeyWhisperMessage : protocolMessages.PreKeyWhisperMessage,
|
||||
DeviceInit : deviceMessages.DeviceInit,
|
||||
IdentityKey : deviceMessages.IdentityKey,
|
||||
DeviceControl : deviceMessages.DeviceControl
|
||||
DeviceControl : deviceMessages.DeviceControl,
|
||||
WebSocketResponseMessage : subProtocolMessages.WebSocketResponseMessage,
|
||||
WebSocketRequestMessage : subProtocolMessages.WebSocketRequestMessage,
|
||||
WebSocketMessage : subProtocolMessages.WebSocketMessage
|
||||
};
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue