diff --git a/client.js b/client.js index f29e962..cea05be 100644 --- a/client.js +++ b/client.js @@ -69,11 +69,6 @@ function Client (opts) { // See: https://github.com/feross/webtorrent-hybrid/issues/46 self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc - // Use a socket pool, so WebSocket tracker clients share WebSocket objects for - // the same server. In practice, WebSockets are pretty slow to establish, so this - // gives a nice performance boost, and saves browser resources. - self._socketPool = {} - var announce = typeof opts.announce === 'string' ? [ opts.announce ] : opts.announce == null ? [] : opts.announce diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index aa7e043..894408e 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -10,6 +10,11 @@ var Socket = require('simple-websocket') var common = require('../common') var Tracker = require('./tracker') +// Use a socket pool, so tracker clients share WebSocket objects for the same server. +// In practice, WebSockets are pretty slow to establish, so this gives a nice performance +// boost, and saves browser resources. +var socketPool = {} + var RECONNECT_MINIMUM = 15 * 1000 var RECONNECT_MAXIMUM = 30 * 60 * 1000 var RECONNECT_VARIANCE = 30 * 1000 @@ -124,15 +129,15 @@ WebSocketTracker.prototype.destroy = function (cb) { self._onSocketDataBound = null self._onSocketCloseBound = null - if (self.client._socketPool[self.announceUrl]) { - self.client._socketPool[self.announceUrl].consumers -= 1 + if (socketPool[self.announceUrl]) { + socketPool[self.announceUrl].consumers -= 1 } // Other instances are using the socket, so there's nothing left to do here - if (self.client._socketPool[self.announceUrl].consumers > 0) return cb() + if (socketPool[self.announceUrl].consumers > 0) return cb() - var socket = self.client._socketPool[self.announceUrl] - delete self.client._socketPool[self.announceUrl] + var socket = socketPool[self.announceUrl] + delete socketPool[self.announceUrl] socket.on('error', noop) // ignore all future errors socket.once('close', cb) @@ -177,11 +182,11 @@ WebSocketTracker.prototype._openSocket = function () { self._onSocketClose() } - self.socket = self.client._socketPool[self.announceUrl] + self.socket = socketPool[self.announceUrl] if (self.socket) { - self.client._socketPool[self.announceUrl].consumers += 1 + socketPool[self.announceUrl].consumers += 1 } else { - self.socket = self.client._socketPool[self.announceUrl] = new Socket(self.announceUrl) + self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl) self.socket.consumers = 1 self.socket.once('connect', self._onSocketConnectBound) }