forked from grandcat/zeroconf
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.go
40 lines (33 loc) · 1.01 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"context"
"flag"
"log"
"time"
"github.com/libp2p/zeroconf/v2"
)
var (
service = flag.String("service", "_workstation._tcp", "Set the service category to look for devices.")
domain = flag.String("domain", "local", "Set the search domain. For local networks, default is fine.")
waitTime = flag.Int("wait", 10, "Duration in [s] to run discovery.")
)
func main() {
flag.Parse()
entries := make(chan *zeroconf.ServiceEntry)
go func(results <-chan *zeroconf.ServiceEntry) {
for entry := range results {
log.Println(entry)
}
log.Println("No more entries.")
}(entries)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*waitTime))
defer cancel()
// Discover all services on the network (e.g. _workstation._tcp)
err := zeroconf.Browse(ctx, *service, *domain, entries)
if err != nil {
log.Fatalln("Failed to browse:", err.Error())
}
<-ctx.Done()
// Wait some additional time to see debug messages on go routine shutdown.
time.Sleep(1 * time.Second)
}