Skip to content

Commit

Permalink
[chore] Use uws as default wsEngine, and ws as fallback (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
kapouer authored and darrachequesne committed Dec 12, 2016
1 parent 10e3802 commit 7e50871
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const REPORTER = 'dot';
gulp.task('default', ['transpile']);

gulp.task('test', ['nsp', 'lint'], function () {
if (parseInt(process.versions.node, 10) < 4 && process.env.EIO_WS_ENGINE === 'uws') {
if (parseInt(process.versions.node, 10) < 4 && process.env.EIO_WS_ENGINE !== 'ws') {
console.info('Node version < 4, skipping tests with uws engine');
process.exit();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/engine.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ function listen (port, options, fn) {
res.end('Not Implemented');
});

server.listen(port, fn);

// create engine server
var engine = exports.attach(server, options);
engine.httpServer = server;

server.listen(port, fn);

return engine;
}

Expand Down
41 changes: 28 additions & 13 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Server (opts) {

opts = opts || {};

this.wsEngine = opts.wsEngine || process.env.EIO_WS_ENGINE;
this.wsEngine = opts.wsEngine || process.env.EIO_WS_ENGINE || 'uws';
this.pingTimeout = opts.pingTimeout || 60000;
this.pingInterval = opts.pingInterval || 25000;
this.upgradeTimeout = opts.upgradeTimeout || 10000;
Expand All @@ -60,18 +60,6 @@ function Server (opts) {
compression.threshold = 1024;
}
});

// initialize websocket server
if (~this.transports.indexOf('websocket')) {
// keep require('ws') as separate expression for packers (browserify, etc)
var WebSocketServer = (this.wsEngine ? require(this.wsEngine) : require('ws')).Server;
this.ws = new WebSocketServer({
noServer: true,
clientTracking: false,
perMessageDeflate: this.perMessageDeflate,
maxPayload: this.maxHttpBufferSize
});
}
}

/**
Expand Down Expand Up @@ -106,6 +94,32 @@ util.inherits(Server, EventEmitter);

Server.prototype.clients;

/**
* Initialize websocket server
*
* @api private
*/

Server.prototype.init = function () {
if (~this.transports.indexOf('websocket')) {
var wsModule;
try {
wsModule = require(this.wsEngine);
} catch (ex) {
this.wsEngine = 'ws';
// keep require('ws') as separate expression for packers (browserify, etc)
wsModule = require('ws');
}
var WebSocketServer = wsModule.Server;
this.ws = new WebSocketServer({
noServer: true,
clientTracking: false,
perMessageDeflate: this.perMessageDeflate,
maxPayload: this.maxHttpBufferSize
});
}
};

/**
* Returns a list of available transports for upgrade given a certain transport.
*
Expand Down Expand Up @@ -424,6 +438,7 @@ Server.prototype.attach = function (server, options) {
var listeners = server.listeners('request').slice(0);
server.removeAllListeners('request');
server.on('close', self.close.bind(self));
server.on('listening', self.init.bind(self));

// add request handler
server.on('request', function (req, res) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
"gulp-nsp": "^2.4.1",
"mocha": "2.3.4",
"s": "0.1.1",
"superagent": "0.15.4",
"uws": "0.4.0"
"superagent": "0.15.4"
},
"optionalDependencies": {
"uws": "0.12.0"
},
"scripts": {
"test": "gulp test; EIO_WS_ENGINE=uws gulp test;"
"test": "gulp test; EIO_WS_ENGINE=ws gulp test;"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 7e50871

Please # to comment.