diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index c5284f0..47845d6 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -18,6 +18,7 @@ var socketPool = {} var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_MINIMUM = 5 * 1000 +var OFFER_TIMEOUT = 60000 inherits(WebSocketTracker, Tracker) @@ -199,11 +200,15 @@ WebSocketTracker.prototype._onSocketData = function (data) { } if (data.answer && data.peer_id) { - peer = self.peers[common.binaryToHex(data.offer_id)] + var offerId = common.binaryToHex(data.offer_id) + peer = self.peers[offerId] if (peer) { peer.id = common.binaryToHex(data.peer_id) peer.signal(data.answer) self.client.emit('peer', peer) + + clearTimeout(peer.trackerTimeout) + self.peers[offerId] = null } else { debug('got unexpected answer: ' + JSON.stringify(data.answer)) } @@ -253,7 +258,6 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) { var offers = [] debug('generating %s offers', numwant) - // TODO: cleanup dead peers and peers that never get a return offer, from self.peers for (var i = 0; i < numwant; ++i) { generateOffer() } @@ -274,6 +278,10 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) { }) checkDone() }) + peer.trackerTimeout = setTimeout(function () { + peer.destroy() + self.peers[offerId] = null + }, OFFER_TIMEOUT) } function checkDone () {