You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on sample of how to use mDNS to find other peers. And well it didn't work very much. I don't know what i'm doing wrong. I'have followed some guidelines from here or here i'm kind of new in network programming, but i really want to know how it works because i think that what you are doing with p2plib , ipfs and so on is really great. here is my code :
package main
import (
"context"
"fmt"
"time"
crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
swarm "github.com/libp2p/go-libp2p-swarm"
disco "github.com/libp2p/go-libp2p/p2p/discovery"
ma "github.com/multiformats/go-multiaddr"
)
type DiscoveryNotifee struct {
h host.Host
}
func (n *DiscoveryNotifee) HandlePeerFound(pi pstore.PeerInfo) {
fmt.Printf("discovered :")
fmt.Println(pi)
}
func makeRandomNode(port int, ctx context.Context) *bhost.BasicHost {
// Ignoring most errors for brevity
// See echo example for more details and better implementation
priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
pid, _ := peer.IDFromPublicKey(pub)
listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
peerStore := pstore.NewPeerstore()
peerStore.AddPrivKey(pid, priv)
peerStore.AddPubKey(pid, pub)
n, _ := swarm.NewNetwork(ctx, []ma.Multiaddr{listen}, pid, peerStore, nil)
host := bhost.New(n)
fmt.Println(host)
return host
}
func main() {
ctxa := context.Background()
ctxb := context.Background()
a := makeRandomNode(9000, ctxa)
b := makeRandomNode(9001, ctxb)
peersNode1 := len(a.Peerstore().Peers())
peersNode2 := len(b.Peerstore().Peers())
_ = peersNode1
_ = peersNode2
sa, err := disco.NewMdnsService(ctxa, a, time.Second/4, "findme")
if err != nil {
panic(err)
}
sb, err := disco.NewMdnsService(ctxb, b, time.Second/4, "findme")
if err != nil {
panic(err)
}
_ = sb
n := &DiscoveryNotifee{a}
sa.RegisterNotifee(n)
time.Sleep(time.Second * 10)
peersNodea := len(a.Peerstore().Peers())
peersNodeb := len(b.Peerstore().Peers())
_ = peersNodea
_ = peersNodeb
err = a.Connect(context.Background(), pstore.PeerInfo{ID: b.ID()})
if err != nil {
panic(err)
}
}
do i need a routed host or something ? i'm a little bit confuse. Sorry for boring you probably with my little problem :-)
Edit : I just saw #257, maybe it's linked if nobody found nothing wrong in my code ? I 'll have a look at zeroconf and i ll try to implement it in discovery instead of mdns from whyrusleeping, but again, i'm a very newbie in that domain :-) and in golang too.
The text was updated successfully, but these errors were encountered:
Actually, i tested the zeroconf library, it works really well. So i'm would like to replace it, in case if nobody did it during this time is there a doc to learn how to upload code on this project ? (i'm ok with git practise , but not in open-source oriented project).
Hello everybody,
I'm working on sample of how to use mDNS to find other peers. And well it didn't work very much. I don't know what i'm doing wrong. I'have followed some guidelines from here or here i'm kind of new in network programming, but i really want to know how it works because i think that what you are doing with p2plib , ipfs and so on is really great. here is my code :
do i need a routed host or something ? i'm a little bit confuse. Sorry for boring you probably with my little problem :-)
The text was updated successfully, but these errors were encountered: