Skip to content

Commit fafbb25

Browse files
committed
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 webtorrent#148
1 parent a09c51d commit fafbb25

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

server.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,13 +713,19 @@ Server.prototype._onWebSocketClose = function (socket) {
713713
socket.peerId = null
714714
socket.infoHashes = null
715715

716-
socket.removeListener('message', socket.onMessageBound)
716+
if (typeof socket.onMessageBound === 'function') {
717+
socket.removeListener('message', socket.onMessageBound)
718+
}
717719
socket.onMessageBound = null
718720

719-
socket.removeListener('error', socket.onErrorBound)
721+
if (typeof socket.onErrorBound === 'function') {
722+
socket.removeListener('error', socket.onErrorBound)
723+
}
720724
socket.onErrorBound = null
721725

722-
socket.removeListener('close', socket.onCloseBound)
726+
if (typeof socket.onCloseBound === 'function') {
727+
socket.removeListener('close', socket.onCloseBound)
728+
}
723729
socket.onCloseBound = null
724730
}
725731

0 commit comments

Comments
 (0)