@@ -168,7 +168,7 @@ Server.prototype.listen = function (/* port, hostname, onlistening */) {
168168
169169Server . prototype . close = function ( cb ) {
170170 var self = this
171- if ( ! cb ) cb = function ( ) { }
171+ if ( ! cb ) cb = noop
172172 debug ( 'close' )
173173
174174 self . listening = false
@@ -298,6 +298,7 @@ Server.prototype.onWebSocketConnection = function (socket, opts) {
298298 var self = this
299299 if ( ! opts ) opts = { }
300300 opts . trustProxy = opts . trustProxy || self . _trustProxy
301+
301302 socket . peerId = null // as hex
302303 socket . infoHashes = [ ] // swarms that this socket is participating in
303304 socket . onSend = self . _onWebSocketSend . bind ( self , socket )
@@ -331,6 +332,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
331332 if ( ! socket . peerId ) socket . peerId = params . peer_id // as hex
332333
333334 self . _onRequest ( params , function ( err , response ) {
335+ if ( self . destroyed ) return
334336 if ( err ) {
335337 socket . send ( JSON . stringify ( {
336338 action : params . action === common . ACTIONS . ANNOUNCE ? 'announce' : 'scrape' ,
@@ -341,7 +343,6 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
341343 self . emit ( 'warning' , err )
342344 return
343345 }
344- if ( self . destroyed ) return
345346
346347 response . action = params . action === common . ACTIONS . ANNOUNCE ? 'announce' : 'scrape'
347348
@@ -612,14 +613,17 @@ Server.prototype._onWebSocketClose = function (socket) {
612613 event : 'stopped' ,
613614 numwant : 0 ,
614615 peer_id : socket . peerId
615- } , function ( ) { } )
616+ } , noop )
616617 }
617618 } )
618619 }
619620
621+ // ignore all future errors
622+ socket . onSend = noop
623+ socket . on ( 'error' , noop )
624+
620625 socket . peerId = null
621626 socket . infoHashes = null
622- socket . onSend = null
623627
624628 socket . removeListener ( 'message' , socket . onMessageBound )
625629 socket . onMessageBound = null
@@ -642,3 +646,5 @@ function toNumber (x) {
642646 x = Number ( x )
643647 return x >= 0 ? x : false
644648}
649+
650+ function noop ( ) { }
0 commit comments