forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
35 lines (32 loc) · 701 Bytes
/
main.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
package main
import (
"context"
"log"
"time"
"github.com/plgd-dev/go-coap/v2/udp"
"github.com/plgd-dev/go-coap/v2/udp/message/pool"
)
func main() {
sync := make(chan bool)
co, err := udp.Dial("localhost:5688")
if err != nil {
log.Fatalf("Error dialing: %v", err)
}
num := 0
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
obs, err := co.Observe(ctx, "/some/path", func(req *pool.Message) {
log.Printf("Got %+v\n", req)
num++
if num >= 10 {
sync <- true
}
})
if err != nil {
log.Fatalf("Unexpected error '%v'", err)
}
<-sync
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
obs.Cancel(ctx)
}