func GetChromecasts(duration time.Duration) ([]*ChromecastInfo, error) { var casts []*ChromecastInfo ch := make(chan *mdns.ServiceEntry, 4) defer close(ch) params := &mdns.QueryParam{ Service: "_googlecast._tcp.local.", Domain: "local", Timeout: duration, Entries: ch, WantUnicastResponse: false, } go func() { for e := range ch { cast := ChromecastInfo{ Name: e.Name, Host: e.Host, Port: e.Port, Addr: e.Addr, AddrV4: e.AddrV4, AddrV6: e.AddrV6, } casts = append(casts, &cast) } }() err := mdns.Query(params) if err != nil { return nil, err } return casts, nil }
// poll is invoked periodically to check for new hosts func (m *AgentMDNS) poll(hosts chan *mdns.ServiceEntry) { params := mdns.QueryParam{ Service: mdnsName(m.discover), Interface: m.iface, Entries: hosts, } if err := mdns.Query(¶ms); err != nil { m.logger.Printf("[ERR] agent.mdns: Failed to poll for new hosts: %v", err) } }
// A simple example, showing how to find a Chromecast using mdns, and request its status. func main() { castService := "_googlecast._tcp" // Make a channel for results and start listening entriesCh := make(chan *mdns.ServiceEntry, 4) go func() { for entry := range entriesCh { if !strings.Contains(entry.Name, castService) { return } fmt.Printf("Got new chromecast: %v\n", entry) client, err := castv2.NewClient(entry.Addr, entry.Port) if err != nil { log.Fatalf("Failed to connect to chromecast %s", entry.Addr) } //_ = controllers.NewHeartbeatController(client, "Tr@n$p0rt-0", "Tr@n$p0rt-0") heartbeat := controllers.NewHeartbeatController(client, "sender-0", "receiver-0") heartbeat.Start() connection := controllers.NewConnectionController(client, "sender-0", "receiver-0") connection.Connect() receiver := controllers.NewReceiverController(client, "sender-0", "receiver-0") response, err := receiver.GetStatus(time.Second * 5) spew.Dump("Status response", response, err) } }() go func() { mdns.Query(&mdns.QueryParam{ Service: castService, Timeout: time.Second * 30, Entries: entriesCh, }) }() c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) // Block until a signal is received. s := <-c fmt.Println("Got signal:", s) }
func mdnsLookup(entriesCh chan *mdns.ServiceEntry) { params := mdns.DefaultParams(castService) params.Entries = entriesCh params.WantUnicastResponse = true mdns.Query(params) }