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
|
||||
};
|
||||
})();
|
||||
|
|
45
protos/SubProtocol.proto
Normal file
45
protos/SubProtocol.proto
Normal file
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* Copyright (C) 2014 Open WhisperSystems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package textsecure;
|
||||
|
||||
option java_package = "org.whispersystems.websocket.messages.protobuf";
|
||||
|
||||
message WebSocketRequestMessage {
|
||||
optional string verb = 1;
|
||||
optional string path = 2;
|
||||
optional bytes body = 3;
|
||||
optional uint64 id = 4;
|
||||
}
|
||||
|
||||
message WebSocketResponseMessage {
|
||||
optional uint64 id = 1;
|
||||
optional uint32 status = 2;
|
||||
optional string message = 3;
|
||||
optional bytes body = 4;
|
||||
}
|
||||
|
||||
message WebSocketMessage {
|
||||
enum Type {
|
||||
UNKNOWN = 0;
|
||||
REQUEST = 1;
|
||||
RESPONSE = 2;
|
||||
}
|
||||
|
||||
optional Type type = 1;
|
||||
optional WebSocketRequestMessage request = 2;
|
||||
optional WebSocketResponseMessage response = 3;
|
||||
}
|
Loading…
Reference in a new issue