Keepalive endpoint

This commit is contained in:
lilia 2014-12-06 19:55:27 -08:00
parent 4f97f8c1e4
commit 2209407d5e

View file

@ -25,7 +25,10 @@
*/ */
window.textsecure.websocket = function (url) { window.textsecure.websocket = function (url) {
var socketWrapper = { onmessage: function() {}, ondisconnect: function() {} }; var socketWrapper = {
onmessage : function() {},
ondisconnect : function() {},
};
var socket; var socket;
var keepAliveTimer; var keepAliveTimer;
var reconnectSemaphore = 0; var reconnectSemaphore = 0;
@ -34,7 +37,13 @@
function resetKeepAliveTimer() { function resetKeepAliveTimer() {
clearTimeout(keepAliveTimer); clearTimeout(keepAliveTimer);
keepAliveTimer = setTimeout(function() { keepAliveTimer = setTimeout(function() {
socket.send(JSON.stringify({type: 1, id: 0})); socket.send(
new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: { verb: 'GET', path: '/v1/keepalive' }
}).encode().toArrayBuffer()
);
resetKeepAliveTimer(); resetKeepAliveTimer();
}, 15000); }, 15000);
}; };
@ -45,7 +54,7 @@
socketWrapper.ondisconnect(e); socketWrapper.ondisconnect(e);
}; };
var connect = function() { function connect() {
clearTimeout(keepAliveTimer); clearTimeout(keepAliveTimer);
if (++reconnectSemaphore <= 0) { return; } if (++reconnectSemaphore <= 0) { return; }
@ -64,7 +73,7 @@
socketWrapper.send = function(msg) { socketWrapper.send = function(msg) {
socket.send(msg); socket.send(msg);
} }
}; }
connect(); connect();
return socketWrapper; return socketWrapper;