Update to WS 6.x and fix all it broke

Significant update to the ws module to get it completely up to date.

The jump from 1.x to 6.x has required a rewrite of our WS handling. Most
specifically the means by which you can have multiple ws servers on a
single http server has completely changed; we now have to handle the
'upgrade' event on the server ourselves.
This commit is contained in:
Nick O'Leary
2019-01-08 16:21:36 +00:00
parent 201d1926bc
commit ed31a0cf15
4 changed files with 79 additions and 55 deletions

View File

@@ -15,6 +15,7 @@
**/
var ws = require("ws");
var url = require("url");
var log = require("@node-red/util").log; // TODO: separate module
var Tokens;
@@ -78,6 +79,7 @@ function CommsConnection(ws) {
addActiveConnection(self);
}
ws.on('close',function() {
console.log(arguments);
log.audit({event: "comms.close",user:self.user, session: self.session});
log.trace("comms.close "+self.session);
removeActiveConnection(self);
@@ -187,27 +189,27 @@ function start() {
Users.default().then(function(_anonymousUser) {
anonymousUser = _anonymousUser;
var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
var path = settings.httpAdminRoot || "/";
path = (path.slice(0,1) != "/" ? "/":"") + path + (path.slice(-1) == "/" ? "":"/") + "comms";
wsServer = new ws.Server({
server:server,
path:path,
// Disable the deflate option due to this issue
// https://github.com/websockets/ws/pull/632
// that is fixed in the 1.x release of the ws module
// that we cannot currently pickup as it drops node 0.10 support
//perMessageDeflate: false
});
var commsPath = settings.httpAdminRoot || "/";
commsPath = (commsPath.slice(0,1) != "/" ? "/":"") + commsPath + (commsPath.slice(-1) == "/" ? "":"/") + "comms";
wsServer = new ws.Server({ noServer: true });
wsServer.on('connection',function(ws) {
var commsConnection = new CommsConnection(ws);
});
wsServer.on('error', function(err) {
log.warn(log._("comms.error-server",{message:err.toString()}));
});
server.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === commsPath) {
wsServer.handleUpgrade(request, socket, head, function done(ws) {
wsServer.emit('connection', ws, request);
});
}
// Don't destroy the socket as other listeners may want to handle the
// event.
});
lastSentTime = Date.now();
heartbeatTimer = setInterval(function() {