@@ -28,6 +28,7 @@ function WebSocketTracker (client, announceUrl, opts) {
2828
2929 self . peers = { } // peers (offer id -> peer)
3030 self . socket = null
31+ self . reconnecting = false
3132
3233 self . _openSocket ( )
3334}
@@ -36,7 +37,7 @@ WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds
3637
3738WebSocketTracker . prototype . announce = function ( opts ) {
3839 var self = this
39- if ( self . destroyed ) return
40+ if ( self . destroyed || self . reconnecting ) return
4041 if ( ! self . socket . connected ) {
4142 return self . socket . once ( 'connect' , self . announce . bind ( self , opts ) )
4243 }
@@ -63,7 +64,7 @@ WebSocketTracker.prototype.announce = function (opts) {
6364
6465WebSocketTracker . prototype . scrape = function ( opts ) {
6566 var self = this
66- if ( self . destroyed ) return
67+ if ( self . destroyed || self . reconnecting ) return
6768 self . _onSocketError ( new Error ( 'scrape not supported ' + self . announceUrl ) )
6869}
6970
@@ -75,10 +76,12 @@ WebSocketTracker.prototype.destroy = function (onclose) {
7576
7677 socketPool [ self . announceUrl ] = null
7778
79+ self . socket . removeListener ( 'connect' , self . _onSocketConnectBound )
7880 self . socket . removeListener ( 'data' , self . _onSocketDataBound )
7981 self . socket . removeListener ( 'close' , self . _onSocketCloseBound )
8082 self . socket . removeListener ( 'error' , self . _onSocketErrorBound )
8183
84+ self . _onSocketConnectBound = null
8285 self . _onSocketErrorBound = null
8386 self . _onSocketDataBound = null
8487 self . _onSocketCloseBound = null
@@ -95,20 +98,34 @@ WebSocketTracker.prototype.destroy = function (onclose) {
9598
9699WebSocketTracker . prototype . _openSocket = function ( ) {
97100 var self = this
101+ self . destroyed = false
102+
103+ self . _onSocketConnectBound = self . _onSocketConnect . bind ( self )
98104 self . _onSocketErrorBound = self . _onSocketError . bind ( self )
99105 self . _onSocketDataBound = self . _onSocketData . bind ( self )
100106 self . _onSocketCloseBound = self . _onSocketClose . bind ( self )
101107
102108 self . socket = socketPool [ self . announceUrl ]
103109 if ( ! self . socket ) {
104110 self . socket = socketPool [ self . announceUrl ] = new Socket ( self . announceUrl )
111+ self . socket . on ( 'connect' , self . _onSocketConnectBound )
105112 }
106113
107114 self . socket . on ( 'data' , self . _onSocketDataBound )
108115 self . socket . on ( 'close' , self . _onSocketCloseBound )
109116 self . socket . on ( 'error' , self . _onSocketErrorBound )
110117}
111118
119+ WebSocketTracker . prototype . _onSocketConnect = function ( ) {
120+ var self = this
121+ if ( self . destroyed ) return
122+
123+ if ( self . reconnecting ) {
124+ self . reconnecting = false
125+ self . announce ( self . client . _defaultAnnounceOpts ( ) )
126+ }
127+ }
128+
112129WebSocketTracker . prototype . _onSocketData = function ( data ) {
113130 var self = this
114131 if ( self . destroyed ) return
@@ -213,8 +230,8 @@ WebSocketTracker.prototype._startReconnectTimer = function () {
213230 var self = this
214231 var ms = Math . floor ( Math . random ( ) * RECONNECT_VARIANCE ) + RECONNECT_MINIMUM
215232
233+ self . reconnecting = true
216234 var reconnectTimer = setTimeout ( function ( ) {
217- self . destroyed = false
218235 self . _openSocket ( )
219236 } , ms )
220237 if ( reconnectTimer . unref ) reconnectTimer . unref ( )
0 commit comments