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
This commit is contained in:
Feross Aboukhadijeh 2016-04-22 16:34:41 -07:00
parent a09c51dffc
commit fafbb25367

View File

@ -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
}