Skip to content

Commit

Permalink
feat(p2p): peers exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 3, 2018
1 parent cce0f9a commit dfd8378
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class p2p {
return

callback({
protocol: 'rats'
protocol: 'rats',
peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port}))
})

// try to connect back
Expand All @@ -45,6 +46,12 @@ class p2p {
port: data.port ? data.port : socket.remotePort
})
}

// add some other peers
if(data.peers && data.peers.length > 0)
{
data.peers.forEach(peer => this.add(peer))
}
})

// ignore local addresses
Expand Down Expand Up @@ -128,7 +135,8 @@ class p2p {
const protocolTimeout = setTimeout(() => rawSocket.destroy(), 7000)
emit('protocol', {
protocol: 'rats',
port: config.spiderPort
port: config.spiderPort,
peers: this.peersList().slice(0, 4).map(peer => ({address: peer.address, port: peer.port}))
}, (data) => {
if(!data || data.protocol != 'rats')
return
Expand All @@ -140,6 +148,12 @@ class p2p {
this.size++;
this.send('peer', this.size)
console.log('new peer', address)

// add some other peers
if(data.peers && data.peers.length > 0)
{
data.peers.forEach(peer => this.add(peer))
}
})
});

Expand Down Expand Up @@ -171,6 +185,11 @@ class p2p {
peer.emit(type, data, callback)
}
}

peersList()
{
return this.peers.filter(peer => !!peer.emit)
}
}

module.exports = p2p

0 comments on commit dfd8378

Please sign in to comment.