From c56d4ec19269cf8241d94c9c59e6782c5fd90372 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 19 Apr 2020 08:55:23 +0200 Subject: [PATCH] Revert "[minor] Set the flag to detect invalid API usage only when needed" This reverts commit 91b5173c776881dc1a229ac5b0ba928dcb775602. --- lib/websocket-server.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/websocket-server.js b/lib/websocket-server.js index 02df08268..882c19486 100644 --- a/lib/websocket-server.js +++ b/lib/websocket-server.js @@ -79,17 +79,18 @@ class WebSocketServer extends EventEmitter { callback ); } else if (options.server) { - if (options.server[kUsedByWebSocketServer]) { + this._server = options.server; + } + + if (this._server) { + if (this._server[kUsedByWebSocketServer]) { throw new Error( 'The HTTP/S server is already being used by another WebSocket server' ); + } else { + this._server[kUsedByWebSocketServer] = true; } - options.server[kUsedByWebSocketServer] = true; - this._server = options.server; - } - - if (this._server) { this._removeListeners = addListeners(this._server, { listening: this.emit.bind(this, 'listening'), error: this.emit.bind(this, 'error'), @@ -143,6 +144,8 @@ class WebSocketServer extends EventEmitter { const server = this._server; if (server) { + server[kUsedByWebSocketServer] = false; + this._removeListeners(); this._removeListeners = this._server = null; @@ -153,8 +156,6 @@ class WebSocketServer extends EventEmitter { server.close(() => this.emit('close')); return; } - - delete server[kUsedByWebSocketServer]; } process.nextTick(emitClose, this);