Curtail over-zealous websocket reconnects

Closes #173

Previously, in the event of a failed websocket auth, we would attempt to
reconnect once a second ad infinitum. This changeset ensures that we
only reconnect automatically if the socket closed 'normally' as
indicated by the code on the socket's CloseEvent. Otherwise, show a
'Websocket closed' error on the inbox view.

Ideally we would show a more contextual error (ie, 'Unauthorized'), but
unfortunately the actual server response code is not available to our
code. It can be observed in the console output from the background page,
but programmatically, we only receive the WebSocket CloseEvent codes
listed here:
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes

The websocket error message is displayed by a normally-hidden but ever
present socket status element. Clicking this element will immediately
refresh the background page, which will try again to open the websocket
connection.
This commit is contained in:
lilia 2015-03-09 13:20:01 -07:00
parent 1321a90667
commit fd6e2954f7
10 changed files with 170 additions and 45 deletions

View file

@ -15905,14 +15905,16 @@ window.axolotl.sessions = {
*/
window.textsecure.websocket = function (url) {
var socketWrapper = {
onmessage : function() {},
ondisconnect : function() {},
};
var socket;
var keepAliveTimer;
var reconnectSemaphore = 0;
var reconnectTimeout = 1000;
var socket;
var socketWrapper = {
onmessage : function() {},
onclose : function() {},
onerror : function() {},
getStatus : function() { return socket.readyState; }
};
function resetKeepAliveTimer() {
clearTimeout(keepAliveTimer);
@ -15928,10 +15930,27 @@ window.axolotl.sessions = {
}, 15000);
};
function reconnect(e) {
reconnectSemaphore--;
setTimeout(connect, reconnectTimeout);
socketWrapper.ondisconnect(e);
function onclose(e) {
if (e.code === 1000) { // CLOSE_NORMAL
reconnectSemaphore--;
setTimeout(connect, reconnectTimeout);
} else {
console.log('websocket closed', e.code);
}
socketWrapper.onclose(e);
};
function onerror(e) {
socketWrapper.onerror(e);
};
function onmessage(response) {
socketWrapper.onmessage(response);
resetKeepAliveTimer();
};
function send(msg) {
socket.send(msg);
};
function connect() {
@ -15941,19 +15960,12 @@ window.axolotl.sessions = {
if (socket) { socket.close(); }
socket = new WebSocket(url);
socket.onerror = reconnect;
socket.onclose = reconnect;
socket.onopen = resetKeepAliveTimer;
socket.onmessage = function(response) {
socketWrapper.onmessage(response);
resetKeepAliveTimer();
};
socketWrapper.send = function(msg) {
socket.send(msg);
}
}
socket.onopen = resetKeepAliveTimer;
socket.onerror = onerror
socket.onclose = onclose;
socket.onmessage = onmessage;
socketWrapper.send = send;
};
connect();
return socketWrapper;