From fafbb25367ee3945ba30696cb45637183d0bc4ec Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 22 Apr 2016 16:34:41 -0700 Subject: [PATCH] Fix "Error: listener must be a function" The websocket can error and close before the "connection" event has fired. In that situation, socket.onMessageBound, etc. will be undefined. Closes #148 --- server.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 04f77c9..45c3220 100644 --- a/server.js +++ b/server.js @@ -713,13 +713,19 @@ Server.prototype._onWebSocketClose = function (socket) { socket.peerId = null socket.infoHashes = null - socket.removeListener('message', socket.onMessageBound) + if (typeof socket.onMessageBound === 'function') { + socket.removeListener('message', socket.onMessageBound) + } socket.onMessageBound = null - socket.removeListener('error', socket.onErrorBound) + if (typeof socket.onErrorBound === 'function') { + socket.removeListener('error', socket.onErrorBound) + } socket.onErrorBound = null - socket.removeListener('close', socket.onCloseBound) + if (typeof socket.onCloseBound === 'function') { + socket.removeListener('close', socket.onCloseBound) + } socket.onCloseBound = null }