@@ -10,6 +10,11 @@ var Socket = require('simple-websocket')
1010var common = require ( '../common' )
1111var Tracker = require ( './tracker' )
1212
13+ // Use a socket pool, so tracker clients share WebSocket objects for the same server.
14+ // In practice, WebSockets are pretty slow to establish, so this gives a nice performance
15+ // boost, and saves browser resources.
16+ var socketPool = { }
17+
1318var RECONNECT_MINIMUM = 15 * 1000
1419var RECONNECT_MAXIMUM = 30 * 60 * 1000
1520var RECONNECT_VARIANCE = 30 * 1000
@@ -124,15 +129,15 @@ WebSocketTracker.prototype.destroy = function (cb) {
124129 self . _onSocketDataBound = null
125130 self . _onSocketCloseBound = null
126131
127- if ( self . client . _socketPool [ self . announceUrl ] ) {
128- self . client . _socketPool [ self . announceUrl ] . consumers -= 1
132+ if ( socketPool [ self . announceUrl ] ) {
133+ socketPool [ self . announceUrl ] . consumers -= 1
129134 }
130135
131136 // Other instances are using the socket, so there's nothing left to do here
132- if ( self . client . _socketPool [ self . announceUrl ] . consumers > 0 ) return cb ( )
137+ if ( socketPool [ self . announceUrl ] . consumers > 0 ) return cb ( )
133138
134- var socket = self . client . _socketPool [ self . announceUrl ]
135- delete self . client . _socketPool [ self . announceUrl ]
139+ var socket = socketPool [ self . announceUrl ]
140+ delete socketPool [ self . announceUrl ]
136141 socket . on ( 'error' , noop ) // ignore all future errors
137142 socket . once ( 'close' , cb )
138143
@@ -177,11 +182,11 @@ WebSocketTracker.prototype._openSocket = function () {
177182 self . _onSocketClose ( )
178183 }
179184
180- self . socket = self . client . _socketPool [ self . announceUrl ]
185+ self . socket = socketPool [ self . announceUrl ]
181186 if ( self . socket ) {
182- self . client . _socketPool [ self . announceUrl ] . consumers += 1
187+ socketPool [ self . announceUrl ] . consumers += 1
183188 } else {
184- self . socket = self . client . _socketPool [ self . announceUrl ] = new Socket ( self . announceUrl )
189+ self . socket = socketPool [ self . announceUrl ] = new Socket ( self . announceUrl )
185190 self . socket . consumers = 1
186191 self . socket . once ( 'connect' , self . _onSocketConnectBound )
187192 }
0 commit comments