Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
/ go-coap Public archive
forked from plgd-dev/go-coap

Implementation of CoAP Server & Client in Go by @Kistler-Group

License

Notifications You must be signed in to change notification settings

toitware/go-coap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov Go Report FOSSA Status

CoAP Client and Server for go

Features supported:

Samples

Simple

Server UDP/TCP

	// Server
	// See /examples/simple/server/main.go
	func handleA(w coap.ResponseWriter, req *coap.Request) {
		log.Printf("Got message in handleA: path=%q: %#v from %v", req.Msg.Path(), req.Msg, req.Client.RemoteAddr())
		w.SetContentFormat(coap.TextPlain)
		log.Printf("Transmitting from A")
		ctx, cancel := context.WithTimeout(req.Ctx, time.Second)
		defer cancel()
		if _, err := w.WriteWithContext(ctx, []byte("hello world")); err != nil {
			log.Printf("Cannot send response: %v", err)
		}
	}

	func main() {
		mux := coap.NewServeMux()
		mux.Handle("/a", coap.HandlerFunc(handleA))

		listenerErrorHandler := func(err error) bool {
			log.Printf("Error occurred on listener: %v", err)
			return true
		}

		log.Fatal(coap.ListenAndServe("udp", ":5688", mux, listenerErrorHandler))
		
		// for tcp
		// log.Fatal(coap.ListenAndServe("tcp", ":5688",  mux, listenerErrorHandler))

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

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

Client

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

		// for udp-dtls
		// co, err := coap.DialDTLS("udp-dtls", "localhost:5688", &dtls.Config{...}, mux))

		if err != nil {
			log.Fatalf("Error dialing: %v", err)
		}

		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()
		resp, err := co.GetWithContext(ctx, path)


		if err != nil {
			log.Fatalf("Error sending request: %v", err)
		}

		log.Printf("Response payload: %v", resp.Payload())
	}

Observe / Notify

Server

Look to examples/observe/server/main.go

Client

Look to examples/observe/client/main.go

Multicast

Server

Look to examples/mcast/server/main.go

Client

Look to examples/mcast/client/main.go

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 './...'

License

Apache 2.0

FOSSA Status

About

Implementation of CoAP Server & Client in Go by @Kistler-Group

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.9%
  • Dockerfile 0.1%