@@ -11,6 +11,9 @@ var Socket = require('simple-websocket')
1111
1212var common = require ( './common' )
1313
14+ var RECONNECT_VARIANCE = 30 * 1000
15+ var RECONNECT_MINIMUM = 5 * 1000
16+
1417inherits ( WebSocketTracker , EventEmitter )
1518
1619function WebSocketTracker ( client , announceUrl , opts ) {
@@ -27,18 +30,13 @@ function WebSocketTracker (client, announceUrl, opts) {
2730 self . _intervalMs = self . client . _intervalMs // use client interval initially
2831 self . _interval = null
2932
30- self . _onSocketErrorBound = self . _onSocketError . bind ( self )
31- self . _onSocketDataBound = self . _onSocketData . bind ( self )
32-
33- self . _socket = new Socket ( announceUrl + '?' + hat ( 40 ) )
34- self . _socket . on ( 'error' , self . _onSocketErrorBound )
35- self . _socket . on ( 'data' , self . _onSocketDataBound )
33+ self . _openSocket ( )
3634}
3735
3836WebSocketTracker . prototype . announce = function ( opts ) {
3937 var self = this
4038 if ( ! self . _socket . connected ) {
41- return self . _socket . on ( 'connect' , self . announce . bind ( self , opts ) )
39+ return self . _socket . once ( 'connect' , self . announce . bind ( self , opts ) )
4240 }
4341
4442 opts . info_hash = self . client . _infoHash . toString ( 'binary' )
@@ -80,15 +78,28 @@ WebSocketTracker.prototype.destroy = function () {
8078 var self = this
8179 if ( self . destroyed ) return
8280 self . destroyed = true
83- self . _socket . removeListener ( 'error' , self . _onSocketErrorBound )
81+
8482 self . _socket . removeListener ( 'data' , self . _onSocketDataBound )
85- self . _socket . close ( )
83+ self . _socket . removeListener ( 'close' , self . _onSocketCloseBound )
84+ self . _socket . removeListener ( 'error' , self . _onSocketErrorBound )
85+ self . _onSocketErrorBound = null
86+ self . _onSocketDataBound = null
87+ self . _onSocketCloseBound = null
88+
89+ try { self . _socket . close ( ) } catch ( e ) { }
90+ self . _socket = null
8691}
8792
88- WebSocketTracker . prototype . _onSocketError = function ( err ) {
93+ WebSocketTracker . prototype . _openSocket = function ( ) {
8994 var self = this
90- if ( self . destroyed ) return
91- self . client . emit ( 'error' , err )
95+ self . _onSocketErrorBound = self . _onSocketError . bind ( self )
96+ self . _onSocketDataBound = self . _onSocketData . bind ( self )
97+ self . _onSocketCloseBound = self . _onSocketClose . bind ( self )
98+
99+ self . _socket = new Socket ( self . _announceUrl + '?' + hat ( 40 ) )
100+ self . _socket . on ( 'data' , self . _onSocketDataBound )
101+ self . _socket . on ( 'close' , self . _onSocketCloseBound )
102+ self . _socket . on ( 'error' , self . _onSocketErrorBound )
92103}
93104
94105WebSocketTracker . prototype . _onSocketData = function ( data ) {
@@ -167,6 +178,22 @@ WebSocketTracker.prototype._onSocketData = function (data) {
167178 }
168179}
169180
181+ WebSocketTracker . prototype . _onSocketClose = function ( ) {
182+ var self = this
183+ self . destroy ( )
184+ var ms = Math . floor ( Math . random ( ) * RECONNECT_VARIANCE ) + RECONNECT_MINIMUM
185+ setTimeout ( function ( ) {
186+ self . _openSocket ( )
187+ } , ms )
188+ debug ( 'reconnecting socket in %s ms' , ms )
189+ }
190+
191+ WebSocketTracker . prototype . _onSocketError = function ( err ) {
192+ var self = this
193+ if ( self . destroyed ) return
194+ self . client . emit ( 'error' , err )
195+ }
196+
170197WebSocketTracker . prototype . _send = function ( params ) {
171198 var self = this
172199 if ( self . destroyed ) return
0 commit comments