2014-11-28 07:03:42 +01:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
/*
|
|
|
|
* var socket = TextSecureWebSocket(url);
|
|
|
|
*
|
|
|
|
* Returns an adamantium-reinforced super socket, capable of sending
|
|
|
|
* app-level keep alives and automatically reconnecting.
|
|
|
|
*
|
|
|
|
*/
|
2014-12-07 04:55:27 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
TextSecureWebSocket = function (url) {
|
|
|
|
'use strict';
|
|
|
|
var keepAliveTimer;
|
|
|
|
var reconnectSemaphore = 0;
|
|
|
|
var reconnectTimeout = 1000;
|
|
|
|
var socket;
|
2015-05-01 02:54:45 +02:00
|
|
|
var calledClose = false;
|
2015-04-30 23:29:10 +02:00
|
|
|
var socketWrapper = {
|
|
|
|
onmessage : function() {},
|
|
|
|
onclose : function() {},
|
|
|
|
onerror : function() {},
|
2015-05-01 02:54:45 +02:00
|
|
|
getStatus : function() { return socket.readyState; },
|
|
|
|
close : function() { calledClose = true; }
|
2015-04-30 23:29:10 +02:00
|
|
|
};
|
|
|
|
var error;
|
2014-11-28 07:03:42 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
function resetKeepAliveTimer() {
|
|
|
|
clearTimeout(keepAliveTimer);
|
2015-05-05 07:26:32 +02:00
|
|
|
if (calledClose) { return; }
|
2015-04-30 23:29:10 +02:00
|
|
|
keepAliveTimer = setTimeout(function() {
|
|
|
|
if (socket.readyState === WebSocket.OPEN) {
|
|
|
|
socket.send(
|
|
|
|
new textsecure.protobuf.WebSocketMessage({
|
|
|
|
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
|
|
|
|
request: { verb: 'GET', path: '/v1/keepalive' }
|
|
|
|
}).encode().toArrayBuffer()
|
|
|
|
);
|
2015-03-11 22:06:29 +01:00
|
|
|
}
|
2015-03-09 21:20:01 +01:00
|
|
|
|
|
|
|
resetKeepAliveTimer();
|
2015-04-30 23:29:10 +02:00
|
|
|
}, 55000);
|
|
|
|
};
|
2015-03-09 21:20:01 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
function onclose(e) {
|
2015-05-01 02:54:45 +02:00
|
|
|
if (!error && !calledClose) {
|
2015-04-30 23:29:10 +02:00
|
|
|
reconnectSemaphore--;
|
|
|
|
setTimeout(connect, reconnectTimeout);
|
|
|
|
}
|
|
|
|
if (e !== 1000 ) { // CLOSE_NORMAL
|
|
|
|
console.log('websocket closed', e.code);
|
|
|
|
}
|
|
|
|
socketWrapper.onclose(e);
|
|
|
|
};
|
2014-11-28 07:03:42 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
function onerror(e) {
|
|
|
|
error = e;
|
|
|
|
console.log('websocket error');
|
|
|
|
socketWrapper.onerror(e);
|
|
|
|
};
|
2014-11-28 07:03:42 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
function onmessage(response) {
|
|
|
|
socketWrapper.onmessage(response);
|
|
|
|
resetKeepAliveTimer();
|
|
|
|
};
|
2014-11-28 07:03:42 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
function send(msg) {
|
|
|
|
resetKeepAliveTimer();
|
|
|
|
socket.send(msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
function connect() {
|
|
|
|
clearTimeout(keepAliveTimer);
|
|
|
|
if (++reconnectSemaphore <= 0) { return; }
|
2014-11-28 07:03:42 +01:00
|
|
|
|
2015-04-30 23:29:10 +02:00
|
|
|
if (socket) { socket.close(); }
|
|
|
|
socket = new WebSocket(url);
|
|
|
|
|
|
|
|
socket.onopen = resetKeepAliveTimer;
|
|
|
|
socket.onerror = onerror
|
|
|
|
socket.onclose = onclose;
|
|
|
|
socket.onmessage = onmessage;
|
|
|
|
socketWrapper.send = send;
|
2014-11-28 07:03:42 +01:00
|
|
|
};
|
2015-04-30 23:29:10 +02:00
|
|
|
|
|
|
|
connect();
|
|
|
|
return socketWrapper;
|
|
|
|
};
|