Minor tweaks and first step towards websocket

This commit is contained in:
Matt Corallo 2014-01-10 21:58:58 -10:00
parent a25cf5f176
commit 8ad510e00e
3 changed files with 39 additions and 7 deletions

View file

@ -34,6 +34,7 @@ var URL_BASE = "http://textsecure-test.herokuapp.com";
var URL_CALLS = {};
URL_CALLS['devices'] = "/v1/devices";
URL_CALLS['keys'] = "/v1/keys";
URL_CALLS['push'] = "/v1/messagesocket";
/**
* REQUIRED PARAMS:
@ -70,11 +71,38 @@ function doAjax(param) {
});
}
function subscribeToPush(user, password, message_callback) {
var request = { url: URL_BASE + URL_CALLS['push'],
contentType: 'application/json',
transport: 'websocket',
fallbackTransport: 'long-polling',
logLevel: 'debug', //TODO
headers: {'Authorization: Basic ' + btoa(user + ":" + password)},
onOpen = function(response) {
console.log('Connected to server using ' + response.transport);
},
onMessage = function(response) {
try {
var message = JSON.parse(response.responseBody);
} catch (e) {
console.log('Error parsing server JSON message: ' + response.responseBody);
return;
}
console.log('Received server message' + message); //TODO
message_callback(message);
},
onError = function(response) {
console.log('Server is down :(');
//TODO: GUI
}};
$.atmosphere.subscribe(request);
}
/*******************************************
*** Utilities to manage keys/randomness ***
*******************************************/
function getRandomBytes(size) {
//TODO: Better random (https://www.grc.com/r&d/js.htm?
//TODO: Better random (https://www.grc.com/r&d/js.htm?)
try {
var array = new Uint8Array(size);
window.crypto.getRandomValues(array);

View file

@ -7,7 +7,7 @@
<h1>TextSecure</h1>
<div id="init-setup" style="display: none;">
<h2>Welcome to TextSecure. To get startet please get a 6-digit setup code from your phone and enter it below.</h2>
Phone number: <input type="text" title="Enter the phone number which you've registered TextSecure with." size="8" id="number" />
Phone number (including country code): +<input type="text" size="2" id="countrycode" /><input type="text" size="8" id="number" />
Code: <input type="text" pattern="[0-9]{3}-?[0-9]{3}" title="Enter the 6-didgit verificaion code displayed on your phone." size="8" id="code" /><br>
<button id="init-go" >Sync</button>
</div>
@ -20,6 +20,7 @@
<h2>You are now registered on TextSecure with number <span id="complete-number"></span></h2>
</div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.atmosphere.js"></script>
<script type="text/javascript" src="helpers.js"></script>
<script type="text/javascript" src="options.js"></script>
</body>

View file

@ -4,7 +4,8 @@ function codeMatches() {
}
function numberMatches() {
return $('#number').value().replace(/\D/g, '').length == 10;
var country_code = $('#countrycode').value().replace(/\D/g, '');
return $('#number').value().replace(/\D/g, '').length > 5 && country_code.length > 0 && country_code.length < 4;
}
$('#code').on('change', function() {
@ -14,7 +15,7 @@ $('#code').on('change', function() {
$('#code').attr('style', '');
});
$('#number').on('change', function() {
$('#number').on('change', function() {//TODO
if (!numberMatches())
$('#number').attr('style', 'background-color:#ff6666;');
else
@ -25,7 +26,7 @@ $('#init-go').click(function() {
if (codeMatches() && numberMatches()) {
var signaling_key = getRandomBytes(32 + 20);
var password = getRandomBytes(16);
var number = $('#number').value().replace(/\D/g, '');
var number = "+" + $('#countrycode').value().replace(/\D/g, '') + $('#number').value().replace(/\D/g, '');
$('#init-setup').hide();
$('#verify1done').html('');
@ -37,11 +38,13 @@ $('#init-go').click(function() {
$('#verify1done').html('done');
var keys = generateKeys();
$('#verify2done').html('done');
doAjax({call: 'keys', httpType: 'PUT', user: number + "." + response, password: password,
var number_id = number + "." + response;
doAjax({call: 'keys', httpType: 'PUT', user: number_id, password: password,
jsonData: keys, success_callback: function(response) {
$('#complete-number').html('');
$('#verify').hide();
$('#setup-complete').show();
storage.putUnencrypted("number_id", number_id);
}, error_callback: function(code) {
alert(code); //TODO
}
@ -71,6 +74,6 @@ $('#init-go').click(function() {
if (storage.getUnencrypted("number_id") === undefined) {
$('#init-setup').show();
} else {
$('#complete-number').html(storage.getUnencrypted("number_id"));
$('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]);
$('#setup-complete').show();
}