Skip to content

Commit

Permalink
feat : add protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzulul committed Jun 17, 2024
1 parent d402e86 commit be1da79
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const prefix = 'webpeerjs'
export const CONFIG_PREFIX = prefix
export const CONFIG_PROTOCOL = '/'+prefix+'/0.0.1'
export const CONFIG_BLOCKSTORE_PATH = prefix+'-blockstore'
export const CONFIG_DATASTORE_PATH = prefix+'-datastore'
export const CONFIG_DBSTORE_PATH = prefix+'-dbstore'
Expand Down
78 changes: 75 additions & 3 deletions src/webpeerjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ class webpeerjs{
//listen to peer connect event
this.#libp2p.addEventListener("peer:connect",async (evt) => {

//console.log(`Connected to ${connection.toString()}`);

const connection = evt.detail;
const id = evt.detail.toString()

//console.log('peer:connect '+id,evt)

const connections = this.#libp2p.getConnections().map((con)=>{return {id:con.remotePeer.toString(),addr:con.remoteAddr.toString()}})
const connect = connections.find((con)=>con.id == id)
const addr = connect.addr
Expand All @@ -159,6 +159,22 @@ class webpeerjs{
},1000)
}

if(this.#webPeersAddrs.has(id)){

let address = [addr]

//update connected webpeers
const now = new Date().getTime()
const metadata = {addrs:address,last:now}
this.#connectedPeers.set(id,metadata)
this.#webPeersAddrs.set(id,address)
this.#connectedPeersArr.length = 0
for(const peer of this.#connectedPeers){
const item = {id:peer[0],address:peer[1].addrs}
this.#connectedPeersArr.push(item)
}
}

});


Expand Down Expand Up @@ -381,7 +397,7 @@ class webpeerjs{
const mddr = multiaddr(addr)
mddrs.push(mddr)
}
//this.#dialMultiaddress(mddrs)
this.#dialMultiaddress(mddrs)
}
}
}
Expand Down Expand Up @@ -467,6 +483,37 @@ class webpeerjs{
this.#ping()
})

this.#libp2p.addEventListener('peer:identify', (evt) => {
//console.log('peer:identify '+evt.detail.peerId.toString(),evt.detail)
if(evt.detail.protocols.includes(config.CONFIG_PROTOCOL)){
//console.log('peer:identify '+evt.detail.peerId.toString(),evt.detail.listenAddrs.toString())

const id = evt.detail.peerId.toString()
let address = []

for(const addrs of evt.detail.listenAddrs){
const addr = addrs.toString()+'/p2p/'+id
if(addr.includes('webtransport')){
address.push(addr)
}
}

//update connected webpeers
const now = new Date().getTime()
const metadata = {addrs:address,last:now}
this.#connectedPeers.set(id,metadata)
this.#webPeersAddrs.set(id,address)
this.#connectedPeersArr.length = 0
for(const peer of this.#connectedPeers){
const item = {id:peer[0],address:peer[1].addrs}
this.#connectedPeersArr.push(item)
}

if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)

}
})

//dial known peers from configuration
this.#dialKnownPeers()

Expand All @@ -482,6 +529,8 @@ class webpeerjs{
//dial random discovered peers
//this.#dialdiscoveredpeers()

this.#registerProtocol()


onMetrics((data)=>{
const signal = metrics(data)
Expand Down Expand Up @@ -555,6 +604,29 @@ class webpeerjs{
PRIVATE FUNCTION
*/

async #registerProtocol(){
const handler = ({ connection, stream, protocol }) => {
// use stream or connection according to the needs
}

await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
maxInboundStreams: 5,
maxOutboundStreams: 5
})

await this.#libp2p.register(config.CONFIG_PROTOCOL, {
onConnect: (peer, connection) => {
// handle connect
//console.log('handle connect',peer)
},
onDisconnect: (peer, connection) => {
// handle disconnect
//console.log('handle disconnect',peer)
}
})

}

#findHybridPeer(){
setTimeout(async()=>{
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
Expand Down

0 comments on commit be1da79

Please sign in to comment.