Skip to content

Commit

Permalink
[fix] Initialize the WebSocket server in the Server constructor (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca authored and darrachequesne committed Jan 30, 2017
1 parent 9b4e983 commit cdb487d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/server.js
Expand Up @@ -61,6 +61,8 @@ function Server (opts) {
compression.threshold = 1024;
}
});

this.init();
}

/**
Expand Down Expand Up @@ -104,23 +106,24 @@ Server.prototype.clients;
*/

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
});
if (!~this.transports.indexOf('websocket')) return;

if (this.ws) this.ws.close();

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');
}
this.ws = new wsModule.Server({
noServer: true,
clientTracking: false,
perMessageDeflate: this.perMessageDeflate,
maxPayload: this.maxHttpBufferSize
});
};

/**
Expand Down
1 change: 1 addition & 0 deletions test/engine.io.js
Expand Up @@ -29,6 +29,7 @@ describe('engine', function () {
it('should create a Server when require called with no arguments', function () {
var engine = eio();
expect(engine).to.be.an(eio.Server);
expect(engine.ws).to.be.ok();
});
});

Expand Down

0 comments on commit cdb487d

Please sign in to comment.