Add examples https://github.com/hashicorp/mdns/pull/42
This commit is contained in:
1
examples/service/.gitignore
vendored
Normal file
1
examples/service/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
service
|
18
examples/service/README.md
Normal file
18
examples/service/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# service
|
||||
|
||||
Compile + run:
|
||||
|
||||
```
|
||||
go build
|
||||
./service
|
||||
```
|
||||
|
||||
[The, run the client](../service)
|
||||
|
||||
|
||||
Use another service tag
|
||||
|
||||
```
|
||||
./service <service-tag>
|
||||
./service _foobarbaz._tcp
|
||||
```
|
45
examples/service/main.go
Normal file
45
examples/service/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/micro/mdns"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
serviceTag := "_foobar._tcp"
|
||||
if len(os.Args) > 1 {
|
||||
serviceTag = os.Args[1]
|
||||
}
|
||||
|
||||
// Setup our service export
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
info := []string{"My awesome service"}
|
||||
service, err := mdns.NewMDNSService(host, serviceTag, "", "", 8000, nil, info)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create the mDNS server, defer shutdown
|
||||
server, err := mdns.NewServer(&mdns.Config{Zone: service})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer server.Shutdown()
|
||||
|
||||
wait()
|
||||
}
|
||||
|
||||
func wait() {
|
||||
ch := make(chan os.Signal)
|
||||
signal.Notify(ch, os.Interrupt, os.Kill)
|
||||
<-ch
|
||||
}
|
Reference in New Issue
Block a user