This commit is contained in:
Asim 2016-04-29 23:53:34 +01:00
parent feb0629af0
commit 3590a367d8
7 changed files with 167 additions and 17 deletions

View File

@ -11,20 +11,42 @@ environments.
Using the library is very simple, here is an example of publishing a service entry: Using the library is very simple, here is an example of publishing a service entry:
```
package main
import (
"github.com/micro/mdns"
"os"
)
func main() {
// Setup our service export // Setup our service export
host, _ := os.Hostname() host, _ := os.Hostname()
info := []string{"My awesome service"}, info := []string{"My awesome service"}
service, _ := NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info) service, _ := mdns.NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info)
// Create the mDNS server, defer shutdown // Create the mDNS server, defer shutdown
server, _ := mdns.NewServer(&mdns.Config{Zone: service}) server, _ := mdns.NewServer(&mdns.Config{Zone: service})
defer server.Shutdown()
defer server.Shutdown()
}
```
Doing a lookup for service providers is also very simple: Doing a lookup for service providers is also very simple:
```
package main
import (
"fmt"
"github.com/cryptix/mdns"
)
func main() {
// Make a channel for results and start listening // Make a channel for results and start listening
entriesCh := make(chan *mdns.ServiceEntry, 4) entriesCh := make(chan *mdns.ServiceEntry, 8)
go func() { go func() {
for entry := range entriesCh { for entry := range entriesCh {
fmt.Printf("Got new entry: %v\n", entry) fmt.Printf("Got new entry: %v\n", entry)
@ -32,6 +54,11 @@ Doing a lookup for service providers is also very simple:
}() }()
// Start the lookup // Start the lookup
mdns.Lookup("_foobar._tcp", entriesCh) err := mdns.Lookup("_foobar._tcp", entriesCh)
close(entriesCh) if err != nil {
fmt.Println(err)
}
close(entriesCh)
}
```

1
examples/client/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
client

17
examples/client/README.md Normal file
View File

@ -0,0 +1,17 @@
# client
[Run the service first](../service)
Then, compile + run:
```
go build
./client
```
Check out other services:
```
./client <service-tag>
./client _foobarbaz._tcp
```

41
examples/client/main.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"os"
"os/signal"
"github.com/micro/mdns"
)
func main() {
serviceTag := "_foobar._tcp"
if len(os.Args) > 1 {
serviceTag = os.Args[1]
}
// Make a channel for results and start listening
entriesCh := make(chan *mdns.ServiceEntry, 8)
defer close(entriesCh)
go func() {
for entry := range entriesCh {
fmt.Printf("Got new entry: %v\n", entry)
}
}()
// Start the lookups
err := mdns.Lookup(serviceTag, entriesCh)
if err != nil {
fmt.Println(err)
}
wait()
}
func wait() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, os.Kill)
<-ch
}

1
examples/service/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
service

View 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
View 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
}