From 543ebae17b629bca2893d4adaf27a271313390a2 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 27 Dec 2023 20:36:33 +0800 Subject: [PATCH 1/3] refactor: call NewStream from only one side --- examples/chat-with-mdns/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/chat-with-mdns/main.go b/examples/chat-with-mdns/main.go index b45e4fd44e..fcd643325b 100644 --- a/examples/chat-with-mdns/main.go +++ b/examples/chat-with-mdns/main.go @@ -116,7 +116,12 @@ func main() { peerChan := initMDNS(host, cfg.RendezvousString) for { // allows multiple peers to join peer := <-peerChan // will block until we discover a peer - fmt.Println("Found peer:", peer, ", connecting") + if peer.ID > host.ID() { + // if other end peer id greater than us, don't connect to it, just wait for it to connect us + fmt.Println("Found peer:", peer, " id is greater than us, wait for it connect us") + continue + } + fmt.Println("Found peer:", peer, ", connecting to it") if err := host.Connect(ctx, peer); err != nil { fmt.Println("Connection failed:", err) From 219a3018e3e3606878591f9f39da4b441afc17f6 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Thu, 28 Dec 2023 11:13:31 +0800 Subject: [PATCH 2/3] Update examples/chat-with-mdns/main.go Co-authored-by: Sukun --- examples/chat-with-mdns/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/chat-with-mdns/main.go b/examples/chat-with-mdns/main.go index fcd643325b..3c742db542 100644 --- a/examples/chat-with-mdns/main.go +++ b/examples/chat-with-mdns/main.go @@ -121,7 +121,7 @@ func main() { fmt.Println("Found peer:", peer, " id is greater than us, wait for it connect us") continue } - fmt.Println("Found peer:", peer, ", connecting to it") + fmt.Println("Found peer:", peer, ", connecting") if err := host.Connect(ctx, peer); err != nil { fmt.Println("Connection failed:", err) From eea24031b714049c0175fa58e38c81f838be3321 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Thu, 28 Dec 2023 11:13:57 +0800 Subject: [PATCH 3/3] Update examples/chat-with-mdns/main.go Co-authored-by: Sukun --- examples/chat-with-mdns/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/chat-with-mdns/main.go b/examples/chat-with-mdns/main.go index 3c742db542..55c36adf40 100644 --- a/examples/chat-with-mdns/main.go +++ b/examples/chat-with-mdns/main.go @@ -118,7 +118,7 @@ func main() { peer := <-peerChan // will block until we discover a peer if peer.ID > host.ID() { // if other end peer id greater than us, don't connect to it, just wait for it to connect us - fmt.Println("Found peer:", peer, " id is greater than us, wait for it connect us") + fmt.Println("Found peer:", peer, " id is greater than us, wait for it to connect to us") continue } fmt.Println("Found peer:", peer, ", connecting")