Watch
1
0
Fork
You've already forked go-coap
0
mirror of https://github.com/matrix-org/go-coap.git synced 2026-11-17 03:30:37 +00:00
Implementation of CoAP Server & Client in Go by @Kistler-Group
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2021-06-08 16:59:19 +01:00
.github fix tests 2021-02-03 08:32:11 +01:00
.vscode implement keepalive feature (#93) 2020-02-07 12:17:08 +01:00
dtls Send blockwise messages as confirmable 2021-06-03 11:38:29 +01:00
examples README and imports 2021-06-01 17:45:43 +01:00
message README and imports 2021-06-01 17:45:43 +01:00
mux README and imports 2021-06-01 17:45:43 +01:00
net Refresh cache expiration when getting new blocks 2021-06-08 16:59:19 +01:00
shared Add logging 2021-06-02 19:22:03 +01:00
tcp Add logging 2021-06-02 19:22:03 +01:00
udp Use cc.writeMessage so we actually do retransmissions 2021-06-07 17:08:30 +01:00
.gitignore Provide context to Request (#36) 2019-03-07 10:42:49 +01:00
Dockerfile Use cc.writeMessage so we actually do retransmissions 2021-06-07 17:08:30 +01:00
go.mod README and imports 2021-06-01 17:45:43 +01:00
go.sum README and imports 2021-06-01 17:45:43 +01:00
LICENSE Update license to apache 2.0 2019-05-24 12:14:49 +02:00
README.md More logging 2021-06-02 19:46:42 +01:00
renovate.json schedule renovate on the first day of the month 2020-01-10 08:52:31 +01:00
server.go README and imports 2021-06-01 17:45:43 +01:00

Go-CoAP

This is the matrix.org fork of go-coap, specialised for MSC3079. There are several reasons to fork the original implementation:

  • FIN packet handling is easier to do at the go-coap level, and is matrix.org specific. See these comments for context.
  • We were hitting many known issues with the original implementation around retry handling, congestion control (NSTART handling), accessing MIDs on UDP messages, etc.
  • We want to add WebSockets support.

This repo was originally forked from an even earlier implementation for FOSDEM 2019, but this new work is based on v2.4.0.

The go-coap provides servers and clients for DTLS, TCP-TLS, UDP, TCP in golang.

Unique features in fork

  • WithLogger to expose internals.

Features

Samples

Simple

Server UDP/TCP

	// Server
	
	// Middleware function, which will be called for each request.
	func loggingMiddleware(next mux.Handler) mux.Handler {
		return mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
			log.Printf("ClientAddress %v, %v\n", w.Client().RemoteAddr(), r.String())
			next.ServeCOAP(w, r)
		})
	}
	
	// See /examples/simple/server/main.go
	func handleA(w mux.ResponseWriter, req *mux.Message) {
		err := w.SetResponse(codes.GET, message.TextPlain, bytes.NewReader([]byte("hello world")))
		if err != nil {
			log.Printf("cannot set response: %v", err)
		}
	}

	func main() {
		r := mux.NewRouter()
		r.Use(loggingMiddleware)
		r.Handle("/a", mux.HandlerFunc(handleA))
		r.Handle("/b", mux.HandlerFunc(handleB))

		log.Fatal(coap.ListenAndServe("udp", ":5688", r))

		
		// for tcp
		// log.Fatal(coap.ListenAndServe("tcp", ":5688",  r))

		// for tcp-tls
		// log.Fatal(coap.ListenAndServeTLS("tcp", ":5688", &tls.Config{...}, r))

		// for udp-dtls
		// log.Fatal(coap.ListenAndServeDTLS("udp", ":5688", &dtls.Config{...}, r))
	}

Client

	// Client
	// See /examples/simpler/client/main.go
	func main() {
		co, err := udp.Dial("localhost:5688")
		
		// for tcp
		// co, err := tcp.Dial("localhost:5688")
		
		// for tcp-tls
		// co, err := tcp.Dial("localhost:5688", tcp.WithTLS(&tls.Config{...}))

		// for dtls
		// co, err := dtls.Dial("localhost:5688", &dtls.Config{...}))

		if err != nil {
			log.Fatalf("Error dialing: %v", err)
		}
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()
		resp, err := co.Get(ctx, "/a")
		if err != nil {
			log.Fatalf("Cannot get response: %v", err)
			return
		}
		log.Printf("Response: %+v", resp)
	}

Observe / Notify

Server example.

Client example.

Multicast

Server example.

Client example.

Contributing

In order to run the tests that the CI will run locally, the following two commands can be used to build the Docker image and run the tests. When making changes, these are the tests that the CI will run, so please make sure that the tests work locally before committing.

$ docker build . --network=host -t go-coap:build --target build
$ docker run --mount type=bind,source="$(pwd)",target=/shared,readonly --network=host go-coap:build go test './...'