22[ ![ codecov] ( https://codecov.io/gh/go-ocf/go-coap/branch/master/graph/badge.svg )] ( https://codecov.io/gh/go-ocf/go-coap )
33[ ![ Go Report] ( https://goreportcard.com/badge/github.com/go-ocf/go-coap )] ( https://goreportcard.com/report/github.com/go-ocf/go-coap )
44[ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap.svg?type=shield )] ( https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap?ref=badge_shield )
5+ [ ![ backer] ( https://opencollective.com/go-coap/backers/badge.svg )] ( https://opencollective.com/go-coap#backer )
6+ [ ![ sponsors] ( https://opencollective.com/go-coap/sponsors/badge.svg )] ( https://opencollective.com/go-coap#sponsors )
7+ [ ![ contributors] ( https://img.shields.io/github/contributors/go-ocf/go-coap )] ( https://github.com/go-ocf/go-coap/graphs/contributors )
8+ [ ![ GitHub stars] ( https://img.shields.io/github/stars/go-ocf/go-coap )] ( https://github.com/go-ocf/go-coap/stargazers )
9+ [ ![ GitHub license] ( https://img.shields.io/github/license/go-ocf/go-coap )] ( https://github.com/go-ocf/go-coap/blob/master/LICENSE )
10+ [ ![ GoDoc] ( https://godoc.org/github.com/go-ocf/go-coap?status.svg )] ( https://godoc.org/github.com/go-ocf/go-coap )
11+ [ ![ Sourcegraph] ( https://sourcegraph.com/github.com/go-ocf/go-coap/-/badge.svg )] ( https://sourcegraph.com/github.com/go-ocf/go-coap?badge )
512
6- # CoAP Client and Server for go
13+ # Go- CoAP
714
8- Features supported:
15+ The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things.
16+ The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.
17+
18+ The go-coap provides servers and clients for DTLS, TCP-TLS, UDP, TCP in golang language.
19+
20+ ## Features
921* CoAP over UDP [ RFC 7252] [ coap ] .
1022* CoAP over TCP/TLS [ RFC 8232] [ coap-tcp ]
1123* Observe resources in CoAP [ RFC 7641] [ coap-observe ]
@@ -30,88 +42,73 @@ Features supported:
3042``` go
3143 // Server
3244 // See /examples/simple/server/main.go
33- func handleA (w coap .ResponseWriter , req *coap .Request ) {
34- log.Printf (" Got message in handleA: path=%q : %#v from %v " , req.Msg .Path (), req.Msg , req.Client .RemoteAddr ())
35- w.SetContentFormat (coap.TextPlain )
36- log.Printf (" Transmitting from A" )
37- ctx , cancel := context.WithTimeout (req.Ctx , time.Second )
38- defer cancel ()
39- if _ , err := w.WriteWithContext (ctx, []byte (" hello world" )); err != nil {
40- log.Printf (" Cannot send response: %v " , err)
45+ func handleA (w mux .ResponseWriter , req *message .Message ) {
46+ log.Printf (" got message in handleA: %+v from %v \n " , req, w.Client ().RemoteAddr ())
47+ err := w.SetResponse (codes.GET , message.TextPlain , bytes.NewReader ([]byte (" hello world" )))
48+ if err != nil {
49+ log.Printf (" cannot set response: %v " , err)
4150 }
4251 }
4352
4453 func main () {
45- mux := coap.NewServeMux ()
46- mux.Handle (" /a" , coap.HandlerFunc (handleA))
54+ m := mux.NewRouter ()
55+ m.Handle (" /a" , mux.HandlerFunc (handleA))
56+ m.Handle (" /b" , mux.HandlerFunc (handleB))
4757
48- listenerErrorHandler := func (err error ) bool {
49- log.Printf (" Error occurred on listener: %v " , err)
50- return true
51- }
58+ log.Fatal (coap.ListenAndServe (" udp" , " :5688" , m))
5259
53- log.Fatal (coap.ListenAndServe (" udp" , " :5688" , mux, listenerErrorHandler))
5460
5561 // for tcp
56- // log.Fatal(coap.ListenAndServe("tcp", ":5688", mux, listenerErrorHandler ))
62+ // log.Fatal(coap.ListenAndServe("tcp", ":5688", m ))
5763
5864 // for tcp-tls
59- // log.Fatal(coap.ListenAndServeTLS("tcp-tls ", ":5688", &tls.Config{...}, mux, listenerErrorHandler ))
65+ // log.Fatal(coap.ListenAndServeTLS("tcp", ":5688", &tls.Config{...}, m ))
6066
6167 // for udp-dtls
62- // log.Fatal(coap.ListenAndServeDTLS("udp-dtls ", ":5688", &dtls.Config{...}, mux, listenerErrorHandler ))
68+ // log.Fatal(coap.ListenAndServeDTLS("udp", ":5688", &dtls.Config{...}, m ))
6369 }
6470```
6571#### Client
6672``` go
6773 // Client
6874 // See /examples/simpler/client/main.go
6975 func main () {
70- co , err := coap .Dial (" udp " , " localhost:5688" )
76+ co , err := udp .Dial (" localhost:5688" )
7177
7278 // for tcp
73- // co, err := coap .Dial("tcp", "localhost:5688")
79+ // co, err := tcp .Dial("localhost:5688")
7480
7581 // for tcp-tls
76- // co, err := coap.DialTLS("tcp-tls", localhost:5688", &tls.Config{...})
82+ // co, err := tcp.Dial(" localhost:5688", tcp.WithTLS( &tls.Config{...}) )
7783
78- // for udp- dtls
79- // co, err := coap.DialDTLS("udp-dtls", " localhost:5688", &dtls.Config{...}, mux ))
84+ // for dtls
85+ // co, err := dtls.Dial(" localhost:5688", &dtls.Config{...}))
8086
8187 if err != nil {
8288 log.Fatalf (" Error dialing: %v " , err)
8389 }
84-
8590 ctx , cancel := context.WithTimeout (context.Background (), time.Second )
8691 defer cancel ()
87- resp , err := co.GetWithContext (ctx, path)
88-
89-
92+ resp , err := co.Get (ctx, " /a" )
9093 if err != nil {
91- log.Fatalf (" Error sending request: %v " , err)
94+ log.Fatalf (" Cannot get response: %v " , err)
95+ return
9296 }
93-
94- log.Printf (" Response payload: %v " , resp.Payload ())
97+ log.Printf (" Response: %+v " , resp)
9598 }
9699```
97100
98-
99101### Observe / Notify
100102
101- #### Server
102- Look to examples/observe/server/main.go
103-
104- #### Client
105- Look to examples/observe/client/main.go
103+ [ Server] ( examples/observe/server/main.go ) example.
106104
105+ [ Client] ( examples/observe/client/main.go ) example.
107106
108107### Multicast
109108
110- #### Server
111- Look to examples/mcast/server/main.go
109+ [ Server] ( examples/mcast/server/main.go ) example.
112110
113- #### Client
114- Look to examples/mcast/client/main.go
111+ [ Client] ( examples/mcast/client/main.go ) example.
115112
116113## Contributing
117114
@@ -126,3 +123,19 @@ $ docker run --mount type=bind,source="$(pwd)",target=/shared,readonly --network
126123Apache 2.0
127124
128125[ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap.svg?type=large )] ( https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap?ref=badge_large )
126+
127+ <h2 align =" center " >Sponsors</h2 >
128+
129+ [ Become a sponsor] ( https://opencollective.com/go-coap#sponsor ) and get your logo on our README on Github with a link to your site.
130+
131+ <div align =" center " >
132+
133+ <a href =" https://opencollective.com/go-coap/sponsor/0/website?requireActive=false " target =" _blank " ><img src =" https://opencollective.com/go-coap/sponsor/0/avatar.svg?requireActive=false " ></a >
134+
135+ </div >
136+
137+ <h2 align =" center " >Backers</h2 >
138+
139+ [ Become a backer] ( https://opencollective.com/go-coap#backer ) and get your image on our README on Github with a link to your site.
140+
141+ <a href =" https://opencollective.com/go-coap/backer/0/website?requireActive=false " target =" _blank " ><img src =" https://opencollective.com/go-coap/backer/0/avatar.svg?requireActive=false " ></a >
0 commit comments