mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-10-16 13:10:47 +08:00
31 lines
782 B
Go
31 lines
782 B
Go
package main
|
|
|
|
import (
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
|
|
)
|
|
|
|
type discoveryNotifee struct {
|
|
PeerChan chan peer.AddrInfo
|
|
}
|
|
|
|
//interface to be called when new peer is found
|
|
func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
|
|
n.PeerChan <- pi
|
|
}
|
|
|
|
//Initialize the MDNS service
|
|
func initMDNS(peerhost host.Host, rendezvous string) chan peer.AddrInfo {
|
|
// An hour might be a long long period in practical applications. But this is fine for us
|
|
ser := mdns.NewMdnsService(peerhost, rendezvous)
|
|
|
|
//register with service so that we get notified about peer discovery
|
|
n := &discoveryNotifee{}
|
|
n.PeerChan = make(chan peer.AddrInfo)
|
|
|
|
ser.RegisterNotifee(n)
|
|
return n.PeerChan
|
|
}
|