Skip to content

Commit

Permalink
fix:fix connectedPeers not update
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzulul committed Jun 14, 2024
1 parent 570f5a1 commit 41fdc62
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
1 change: 0 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebpeerJS</title>
</head>
Expand Down
12 changes: 8 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ export const CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS = [
]

export const CONFIG_KNOWN_BOOTSTRAP_PEER_IDS = [
'12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
'12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
'12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
'12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
'QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
'QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
'QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt',
'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
'12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
'12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
'12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
'12D3KooWHh98YpAkJsn3ULjMjK1n9QVkXmi8Sb3gTDMatHxCmDP5',
'12D3KooWS79EhkPU7ESUwgG4vyHHzW9FDNZLoWVth9b5N5NSrvaj',
'12D3KooWBbkCD5MpJhMc1mfPAVGEyVkQnyxPKGS7AHwDqQM2JUsk',
'12D3KooWKLdecs31Zmo2pLBjR9HY2vWo3VwM4eBm21Czeucbe6FL',
'12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
]

export const CONFIG_KNOWN_BOOTSTRAP_PUBLIC = [
'12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr'
]
55 changes: 42 additions & 13 deletions src/webpeerjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class webpeerjs{
this.#connections.set(id,addr)

//required by joinRoom version 1 to announce via universal connectivity
if(connection.toString() === config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]){
if(config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC.includes(connection.toString())){
setTimeout(()=>{
this.#announce()
setTimeout(()=>{
Expand Down Expand Up @@ -188,6 +188,11 @@ class webpeerjs{
const now = new Date().getTime()
const metadata = {addrs:address,last:now}
this.#connectedPeers.set(senderPeerId,metadata)
this.#connectedPeersArr.length = 0
for(const peer of this.#connectedPeers){
const item = {id:peer[0],address:peer[1].addrs}
this.#connectedPeersArr.push(item)
}
}

//dial if not connected
Expand Down Expand Up @@ -489,16 +494,6 @@ class webpeerjs{
this.#trackLastSeen()
},5e3)

setInterval(async()=>{
/*const peerId = peerIdFromString('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
//console.info(peerInfo)
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
console.info(event)
}*/
},5e3)

/*setTimeout(async()=>{
try{
//console.log('getClosestPeers')
Expand Down Expand Up @@ -543,6 +538,37 @@ class webpeerjs{
PRIVATE FUNCTION
*/

#findPublicPeer(){
setTimeout(async()=>{
const target = config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC[0]
if(!this.#isConnected(target)){
const peerId = peerIdFromString('12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr')
//const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)

//console.info(peerInfo)
for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
//console.info('findPeer',event)
if (event.name === 'FINAL_PEER'){
//console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
let mddrs = []
let addrs = []
const id = event.peer.id.toString()
for(const mddr of event.peer.multiaddrs){
const peeraddr = mddr.toString()+'/p2p/'+id
const peermddr = multiaddr(peeraddr)
addrs.push(peeraddr)
mddrs.push(peermddr)
}
this.#dialedKnownBootstrap.set(id,addrs)
if(!this.#isConnected(id)){
this.#dialMultiaddress(mddrs)
}
}
}
}
},30e3)
}


//check the last seen in web peer
#trackLastSeen(){
Expand Down Expand Up @@ -604,7 +630,7 @@ class webpeerjs{
return
}

if(this.#webPeersId.includes(id) || id == config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0] ){
if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS.includes(id) ){
this.#dialQueue.unshift(mddrs)
}
else{
Expand Down Expand Up @@ -762,7 +788,9 @@ class webpeerjs{
ids.push(keys[randomKey])

//universal connectivity id for webpeer discovery and joinRoom version 1 to work
ids.push(config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0])
for(const id of config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC){
ids.push(id)
}

for(const id of ids){
if(id == undefined)continue
Expand Down Expand Up @@ -926,6 +954,7 @@ class webpeerjs{
//if(peers == 0){
//currently not needed
this.#dialKnownID()
this.#findPublicPeer()
setTimeout(()=>{
const peers = this.#libp2p.getPeers().length
if(peers == 0){
Expand Down

0 comments on commit 41fdc62

Please sign in to comment.