@@ -301,9 +301,15 @@ Server.prototype.onWebSocketConnection = function (socket, opts) {
301301 socket . peerId = null // as hex
302302 socket . infoHashes = [ ] // swarms that this socket is participating in
303303 socket . onSend = self . _onWebSocketSend . bind ( self , socket )
304- socket . on ( 'message' , self . _onWebSocketRequest . bind ( self , socket , opts ) )
305- socket . on ( 'error' , self . _onWebSocketError . bind ( self , socket ) )
306- socket . on ( 'close' , self . _onWebSocketClose . bind ( self , socket ) )
304+
305+ socket . onMessageBound = self . _onWebSocketRequest . bind ( self , socket , opts )
306+ socket . on ( 'message' , self . onMessageBound )
307+
308+ socket . onErrorBound = self . _onWebSocketError . bind ( self , socket )
309+ socket . on ( 'error' , self . onErrorBound )
310+
311+ socket . onCloseBound = self . _onWebSocketClose . bind ( self , socket )
312+ socket . on ( 'close' , self . onCloseBound )
307313}
308314
309315Server . prototype . _onWebSocketRequest = function ( socket , opts , params ) {
@@ -598,19 +604,27 @@ Server.prototype._onWebSocketSend = function (socket, err) {
598604
599605Server . prototype . _onWebSocketClose = function ( socket ) {
600606 var self = this
601- if ( ! socket . peerId || ! socket . infoHashes ) return
602- debug ( 'websocket close' )
607+ debug ( 'websocket close %s' , socket . peerId )
608+
609+ if ( socket . peerId ) {
610+ socket . infoHashes . forEach ( function ( infoHash ) {
611+ var swarm = self . torrents [ infoHash ]
612+ if ( swarm ) {
613+ swarm . announce ( {
614+ event : 'stopped' ,
615+ numwant : 0 ,
616+ peer_id : socket . peerId
617+ } , function ( ) { } )
618+ }
619+ } )
620+ }
603621
604- socket . infoHashes . forEach ( function ( infoHash ) {
605- var swarm = self . torrents [ infoHash ]
606- if ( swarm ) {
607- swarm . announce ( {
608- event : 'stopped' ,
609- numwant : 0 ,
610- peer_id : socket . peerId
611- } , function ( ) { } )
612- }
613- } )
622+ socket . peerId = null
623+ socket . infoHashes = null
624+ socket . onSend = null
625+ socket . removeListener ( 'message' , socket . onMessageBound )
626+ socket . removeListener ( 'error' , socket . onErrorBound )
627+ socket . removeListener ( 'close' , socket . onCloseBound )
614628}
615629
616630Server . prototype . _onWebSocketError = function ( socket , err ) {
0 commit comments