From cdb487dbdcbf97e771e232fd65b594172fc947b5 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 30 Jan 2017 15:52:00 +0100 Subject: [PATCH] [fix] Initialize the WebSocket server in the `Server` constructor (#476) --- lib/server.js | 35 +++++++++++++++++++---------------- test/engine.io.js | 1 + 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/server.js b/lib/server.js index 0da4c234c8..99dfb4150e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -61,6 +61,8 @@ function Server (opts) { compression.threshold = 1024; } }); + + this.init(); } /** @@ -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 + }); }; /** diff --git a/test/engine.io.js b/test/engine.io.js index 4850c5204e..fea40f037f 100644 --- a/test/engine.io.js +++ b/test/engine.io.js @@ -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(); }); });