only return correct peer type from swarm

This commit is contained in:
Feross Aboukhadijeh 2015-06-06 13:10:49 -07:00
parent ad0cd4edab
commit 96f6fce1ab

View File

@ -20,10 +20,11 @@ Swarm.prototype.announce = function (params, cb) {
if (self[fn]) {
self[fn](params, peer) // process event
var peerType = params.compact === undefined ? 'webrtc' : 'addr'
cb(null, {
complete: self.complete,
incomplete: self.incomplete,
peers: self._getPeers(params.numwant)
peers: self._getPeers(params.numwant, peerType)
})
} else {
cb(new Error('invalid event'))
@ -93,13 +94,16 @@ Swarm.prototype._onAnnounce_update = function (params, peer) {
}
}
Swarm.prototype._getPeers = function (numWant) {
Swarm.prototype._getPeers = function (numWant, peerType) {
var peers = []
var ite = randomIterate(Object.keys(this.peers))
while (true) {
var peerId = ite()
if (peers.length >= numWant || peerId == null) return peers
var peer = this.peers[peerId]
if (peer) peers.push(peer)
if (peer &&
((peerType === 'webrtc' && peer.socket) || (peerType === 'addr' && peer.ip))) {
peers.push(peer)
}
}
}