client: websocket-tracker: announce on reconnect

when coming back from being offline, or losing the websocket
connection, announcing immediately means peers will be found faster
(for us and remote peers who may be sitting at 0 peers)
This commit is contained in:
Feross Aboukhadijeh 2015-07-29 03:56:22 -07:00
parent 7c0bbcddd4
commit 6ef2d98b07

View File

@ -28,6 +28,7 @@ function WebSocketTracker (client, announceUrl, opts) {
self.peers = {} // peers (offer id -> peer)
self.socket = null
self.reconnecting = false
self._openSocket()
}
@ -36,7 +37,7 @@ WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds
WebSocketTracker.prototype.announce = function (opts) {
var self = this
if (self.destroyed) return
if (self.destroyed || self.reconnecting) return
if (!self.socket.connected) {
return self.socket.once('connect', self.announce.bind(self, opts))
}
@ -63,7 +64,7 @@ WebSocketTracker.prototype.announce = function (opts) {
WebSocketTracker.prototype.scrape = function (opts) {
var self = this
if (self.destroyed) return
if (self.destroyed || self.reconnecting) return
self._onSocketError(new Error('scrape not supported ' + self.announceUrl))
}
@ -75,10 +76,12 @@ WebSocketTracker.prototype.destroy = function (onclose) {
socketPool[self.announceUrl] = null
self.socket.removeListener('connect', self._onSocketConnectBound)
self.socket.removeListener('data', self._onSocketDataBound)
self.socket.removeListener('close', self._onSocketCloseBound)
self.socket.removeListener('error', self._onSocketErrorBound)
self._onSocketConnectBound = null
self._onSocketErrorBound = null
self._onSocketDataBound = null
self._onSocketCloseBound = null
@ -95,6 +98,9 @@ WebSocketTracker.prototype.destroy = function (onclose) {
WebSocketTracker.prototype._openSocket = function () {
var self = this
self.destroyed = false
self._onSocketConnectBound = self._onSocketConnect.bind(self)
self._onSocketErrorBound = self._onSocketError.bind(self)
self._onSocketDataBound = self._onSocketData.bind(self)
self._onSocketCloseBound = self._onSocketClose.bind(self)
@ -102,6 +108,7 @@ WebSocketTracker.prototype._openSocket = function () {
self.socket = socketPool[self.announceUrl]
if (!self.socket) {
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
self.socket.on('connect', self._onSocketConnectBound)
}
self.socket.on('data', self._onSocketDataBound)
@ -109,6 +116,16 @@ WebSocketTracker.prototype._openSocket = function () {
self.socket.on('error', self._onSocketErrorBound)
}
WebSocketTracker.prototype._onSocketConnect = function () {
var self = this
if (self.destroyed) return
if (self.reconnecting) {
self.reconnecting = false
self.announce(self.client._defaultAnnounceOpts())
}
}
WebSocketTracker.prototype._onSocketData = function (data) {
var self = this
if (self.destroyed) return
@ -213,8 +230,8 @@ WebSocketTracker.prototype._startReconnectTimer = function () {
var self = this
var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + RECONNECT_MINIMUM
self.reconnecting = true
var reconnectTimer = setTimeout(function () {
self.destroyed = false
self._openSocket()
}, ms)
if (reconnectTimer.unref) reconnectTimer.unref()