prevent two reconnect timers for running at same time

This commit is contained in:
Feross Aboukhadijeh 2016-03-18 23:14:48 -07:00
parent 49025c1320
commit 89b9fb7f40

View File

@ -29,6 +29,7 @@ function WebSocketTracker (client, announceUrl, opts) {
self.peers = {} // peers (offer id -> peer)
self.socket = null
self.reconnecting = false
self.retries = 0
self.reconnectTimer = null
@ -91,7 +92,9 @@ WebSocketTracker.prototype.destroy = function (cb) {
var self = this
if (!cb) cb = noop
if (self.destroyed) return cb(null)
self.destroyed = true
clearInterval(self.interval)
clearTimeout(self.reconnectTimer)
@ -319,6 +322,7 @@ WebSocketTracker.prototype._startReconnectTimer = function () {
var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM)
self.reconnecting = true
clearTimeout(self.reconnectTimer)
self.reconnectTimer = setTimeout(function () {
self.retries++
self._openSocket()