Integrate keepalives into websocket resources
This commit is contained in:
parent
7d2b41db11
commit
cd363bd84d
6 changed files with 116 additions and 118 deletions
|
@ -53,7 +53,6 @@ module.exports = function(grunt) {
|
|||
'libtextsecure/protobufs.js',
|
||||
'libtextsecure/websocket.js',
|
||||
'libtextsecure/websocket-resources.js',
|
||||
'libtextsecure/keepalive.js',
|
||||
'libtextsecure/helpers.js',
|
||||
'libtextsecure/stringview.js',
|
||||
'libtextsecure/api.js',
|
||||
|
|
|
@ -38563,7 +38563,7 @@ TextSecureWebSocket = function (url, opts) {
|
|||
);
|
||||
};
|
||||
|
||||
window.WebSocketResource = function(socket, handleRequest) {
|
||||
window.WebSocketResource = function(socket, handleRequest, keepalive) {
|
||||
this.sendRequest = function(options) {
|
||||
return new OutgoingWebSocketRequest(options, socket);
|
||||
};
|
||||
|
@ -38609,63 +38609,60 @@ TextSecureWebSocket = function (url, opts) {
|
|||
};
|
||||
reader.readAsArrayBuffer(blob);
|
||||
};
|
||||
|
||||
if (opts.keepalive) {
|
||||
var keepalive = new KeepAlive(this, {
|
||||
path : opts.keepalive.path,
|
||||
disconnect : opts.keepalive.disconnect
|
||||
});
|
||||
|
||||
this.resetKeepAliveTimer = keepalive.reset.bind(keepalive);
|
||||
}
|
||||
};
|
||||
|
||||
function KeepAlive(websocketResource, opts) {
|
||||
if (websocketResource instanceof WebSocketResource) {
|
||||
opts = opts || {};
|
||||
this.path = opts.path;
|
||||
if (this.path === undefined) {
|
||||
this.path = '/';
|
||||
}
|
||||
this.disconnect = opts.disconnect;
|
||||
if (this.disconnect === undefined) {
|
||||
this.disconnect = false;
|
||||
}
|
||||
this.wsr = websocketResource;
|
||||
this.reset();
|
||||
} else {
|
||||
throw new TypeError('KeepAlive expected a WebSocketResource');
|
||||
}
|
||||
}
|
||||
|
||||
KeepAlive.prototype = {
|
||||
constructor: KeepAlive,
|
||||
reset: function() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
this.keepAliveTimer = setTimeout(function() {
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: this.path,
|
||||
success: this.reset.bind(this)
|
||||
});
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(function() {
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}.bind(this), 1000);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
}.bind(this), 55000);
|
||||
},
|
||||
};
|
||||
|
||||
}());
|
||||
|
||||
/* 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/>.
|
||||
*/
|
||||
|
||||
function KeepAlive(websocketResource, opts) {
|
||||
if (websocketResource instanceof WebSocketResource) {
|
||||
opts = opts || {};
|
||||
this.disconnect = opts.disconnect;
|
||||
if (this.disconnect === undefined) {
|
||||
this.disconnect = true;
|
||||
}
|
||||
this.wsr = websocketResource;
|
||||
this.reset();
|
||||
} else {
|
||||
throw new TypeError('KeepAlive expected a WebSocketResource');
|
||||
}
|
||||
}
|
||||
|
||||
KeepAlive.prototype = {
|
||||
constructor: KeepAlive,
|
||||
reset: function() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
this.keepAliveTimer = setTimeout(function() {
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: '/v1/keepalive',
|
||||
success: this.reset.bind(this)
|
||||
});
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(function() {
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}.bind(this), 1000);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
}.bind(this), 55000);
|
||||
},
|
||||
};
|
||||
|
||||
/* vim: ts=4:sw=4:expandtab
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
@ -39411,8 +39408,9 @@ TextSecureServer = function () {
|
|||
} else {
|
||||
console.log('Unknown websocket message', request.path);
|
||||
}
|
||||
}, {
|
||||
keepalive: { path: '/v1/keepalive' }
|
||||
});
|
||||
new KeepAlive(wsr, { disconnect: false });
|
||||
});
|
||||
}).then(function() {
|
||||
return generateKeys(100, progressCallback);
|
||||
|
@ -39557,14 +39555,15 @@ function generateKeys(count, progressCallback) {
|
|||
eventTarget.dispatchEvent(ev);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this));
|
||||
this.keepalive = new KeepAlive(this.wsr);
|
||||
this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), {
|
||||
keepalive: { path: '/v1/keepalive', disconnect: true }
|
||||
});
|
||||
|
||||
},
|
||||
handleRequest: function(request) {
|
||||
this.keepalive.reset();
|
||||
this.wsr.resetKeepAliveTimer();
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
|
|
|
@ -72,8 +72,9 @@
|
|||
} else {
|
||||
console.log('Unknown websocket message', request.path);
|
||||
}
|
||||
}, {
|
||||
keepalive: { path: '/v1/keepalive' }
|
||||
});
|
||||
new KeepAlive(wsr, { disconnect: false });
|
||||
});
|
||||
}).then(function() {
|
||||
return generateKeys(100, progressCallback);
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
function KeepAlive(websocketResource, opts) {
|
||||
if (websocketResource instanceof WebSocketResource) {
|
||||
opts = opts || {};
|
||||
this.disconnect = opts.disconnect;
|
||||
if (this.disconnect === undefined) {
|
||||
this.disconnect = true;
|
||||
}
|
||||
this.wsr = websocketResource;
|
||||
this.reset();
|
||||
} else {
|
||||
throw new TypeError('KeepAlive expected a WebSocketResource');
|
||||
}
|
||||
}
|
||||
|
||||
KeepAlive.prototype = {
|
||||
constructor: KeepAlive,
|
||||
reset: function() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
this.keepAliveTimer = setTimeout(function() {
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: '/v1/keepalive',
|
||||
success: this.reset.bind(this)
|
||||
});
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(function() {
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}.bind(this), 1000);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
}.bind(this), 55000);
|
||||
},
|
||||
};
|
|
@ -42,14 +42,15 @@
|
|||
eventTarget.dispatchEvent(ev);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this));
|
||||
this.keepalive = new KeepAlive(this.wsr);
|
||||
this.wsr = new WebSocketResource(this.socket, this.handleRequest.bind(this), {
|
||||
keepalive: { path: '/v1/keepalive', disconnect: true }
|
||||
});
|
||||
|
||||
},
|
||||
handleRequest: function(request) {
|
||||
this.keepalive.reset();
|
||||
this.wsr.resetKeepAliveTimer();
|
||||
// TODO: handle different types of requests. for now we only expect
|
||||
// PUT /messages <encrypted IncomingPushMessageSignal>
|
||||
textsecure.crypto.decryptWebsocketMessage(request.body).then(function(plaintext) {
|
||||
|
|
|
@ -92,7 +92,7 @@
|
|||
);
|
||||
};
|
||||
|
||||
window.WebSocketResource = function(socket, handleRequest) {
|
||||
window.WebSocketResource = function(socket, handleRequest, keepalive) {
|
||||
this.sendRequest = function(options) {
|
||||
return new OutgoingWebSocketRequest(options, socket);
|
||||
};
|
||||
|
@ -138,6 +138,56 @@
|
|||
};
|
||||
reader.readAsArrayBuffer(blob);
|
||||
};
|
||||
|
||||
if (opts.keepalive) {
|
||||
var keepalive = new KeepAlive(this, {
|
||||
path : opts.keepalive.path,
|
||||
disconnect : opts.keepalive.disconnect
|
||||
});
|
||||
|
||||
this.resetKeepAliveTimer = keepalive.reset.bind(keepalive);
|
||||
}
|
||||
};
|
||||
|
||||
function KeepAlive(websocketResource, opts) {
|
||||
if (websocketResource instanceof WebSocketResource) {
|
||||
opts = opts || {};
|
||||
this.path = opts.path;
|
||||
if (this.path === undefined) {
|
||||
this.path = '/';
|
||||
}
|
||||
this.disconnect = opts.disconnect;
|
||||
if (this.disconnect === undefined) {
|
||||
this.disconnect = false;
|
||||
}
|
||||
this.wsr = websocketResource;
|
||||
this.reset();
|
||||
} else {
|
||||
throw new TypeError('KeepAlive expected a WebSocketResource');
|
||||
}
|
||||
}
|
||||
|
||||
KeepAlive.prototype = {
|
||||
constructor: KeepAlive,
|
||||
reset: function() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
this.keepAliveTimer = setTimeout(function() {
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: this.path,
|
||||
success: this.reset.bind(this)
|
||||
});
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(function() {
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}.bind(this), 1000);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
}.bind(this), 55000);
|
||||
},
|
||||
};
|
||||
|
||||
}());
|
||||
|
|
Loading…
Reference in a new issue