Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update chat with rendezvous example #2769

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
27 changes: 9 additions & 18 deletions examples/chat-with-rendezvous/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"flag"
"fmt"
"os"
"sync"
"time"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
Expand Down Expand Up @@ -114,7 +114,12 @@ func main() {
// DHT, so that the bootstrapping node of the DHT can go down without
// inhibiting future peer discovery.
ctx := context.Background()
kademliaDHT, err := dht.New(ctx, host)
bootstrapPeers := make([]peer.AddrInfo, len(config.BootstrapPeers))
for i, addr := range config.BootstrapPeers {
peerinfo, _ := peer.AddrInfoFromP2pAddr(addr)
bootstrapPeers[i] = *peerinfo
}
kademliaDHT, err := dht.New(ctx, host, dht.BootstrapPeers(bootstrapPeers...))
if err != nil {
panic(err)
}
Expand All @@ -126,22 +131,8 @@ func main() {
panic(err)
}

// Let's connect to the bootstrap nodes first. They will tell us about the
// other nodes in the network.
var wg sync.WaitGroup
for _, peerAddr := range config.BootstrapPeers {
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
wg.Add(1)
go func() {
defer wg.Done()
if err := host.Connect(ctx, *peerinfo); err != nil {
logger.Warning(err)
} else {
logger.Info("Connection established with bootstrap node:", *peerinfo)
}
}()
}
wg.Wait()
// Wait a bit to let bootstrapping finish (really bootstrap should block until it's ready, but that isn't the case yet.)
time.Sleep(1 * time.Second)

// We use a rendezvous point "meet me here" to announce our location.
// This is like telling your friends to meet you at the Eiffel Tower.
Expand Down
Loading