Skip to content

Commit

Permalink
examples: connect to all peers in example mdns chat app (#1798)
Browse files Browse the repository at this point in the history
* Fixed bug in example mdns chat app.

* Added continue, so that if connection to other host fails, will go back to discovering.
  • Loading branch information
karthik340 authored Oct 9, 2022
1 parent 7e206bd commit 63b8803
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions examples/chat-with-mdns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ func main() {
fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, host.ID().Pretty())

peerChan := initMDNS(host, cfg.RendezvousString)
for { // allows multiple peers to join
peer := <-peerChan // will block untill we discover a peer
fmt.Println("Found peer:", peer, ", connecting")

peer := <-peerChan // will block untill we discover a peer
fmt.Println("Found peer:", peer, ", connecting")

if err := host.Connect(ctx, peer); err != nil {
fmt.Println("Connection failed:", err)
}
if err := host.Connect(ctx, peer); err != nil {
fmt.Println("Connection failed:", err)
continue
}

// open a stream, this stream will be handled by handleStream other end
stream, err := host.NewStream(ctx, peer.ID, protocol.ID(cfg.ProtocolID))
// open a stream, this stream will be handled by handleStream other end
stream, err := host.NewStream(ctx, peer.ID, protocol.ID(cfg.ProtocolID))

if err != nil {
fmt.Println("Stream open failed", err)
} else {
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
if err != nil {
fmt.Println("Stream open failed", err)
} else {
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))

go writeData(rw)
go readData(rw)
fmt.Println("Connected to:", peer)
go writeData(rw)
go readData(rw)
fmt.Println("Connected to:", peer)
}
}

select {} // wait here
}

0 comments on commit 63b8803

Please sign in to comment.