Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

fix: connect to remote peer and show status #2

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/nextjs/src/lib/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function startLibp2p() {

// libp2p is the networking layer that underpins Helia
const libp2p = await createLibp2p({
connectionManager: { autoDial: false },
// connectionManager: { autoDial: false },
dht: kadDHT(),
datastore,
transports: [webTransport(), webSockets()],
Expand All @@ -61,7 +61,7 @@ export async function startLibp2p() {
],
}),
],
peerRouters: [delegatedPeerRouting(client)],
// peerRouters: [delegatedPeerRouting(client)],
})

console.log(`this nodes peerID: ${libp2p.peerId.toString()}`)
Expand Down Expand Up @@ -115,7 +115,9 @@ export const connectToMultiaddrs =
multiaddr = addPeerIdToWebTransportMultiAddr(multiaddr, peerId)
console.log(`dialling: ${multiaddr.toString()}`)
try {
conns.push(await libp2p.dial(multiaddr))
const conn = await libp2p.dial(multiaddr)
conns.push(conn)
console.info('connected to', conn.remotePeer, 'on', conn.remoteAddr)
} catch (e) {
errs.push(e)
console.error(e)
Expand Down
6 changes: 6 additions & 0 deletions packages/nextjs/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export default function Home() {
if (multiaddrs) {
const connections = await connectToMultiaddrs(libp2p)(multiaddrs, peerID)
console.log('connections: ', connections)

if (connections.find(conn => {
return conn.remotePeer.toString() === peerID
})) {
setIsConnected(true)
}
}
} catch (e) {
console.error(e)
Expand Down