You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using a custom coap browser to discover what resouces are registered under a given coap server. In another word, list the paths available just like the one comes when we enter this on a coap browser bar: coap.me/.well-known/core
But the simple server code you provided is not replying the list of url extension. I looked inside the code but I couldn't find any information. For now, I have managed to return one path when requested at /.well-known/core by changing the sever code like below. For next, could you tell me if the discovery feature I want is implemented in the library? if yes, please give me an example on how I can use it. In addition, I am checking if the libraryis it possible to change the registered paths on while the server is running?
Thanks in advance
package main
import (
"bytes""fmt""io""log""strings""time"
coap "github.com/plgd-dev/go-coap/v2""github.com/plgd-dev/go-coap/v2/message""github.com/plgd-dev/go-coap/v2/message/codes""github.com/plgd-dev/go-coap/v2/mux"
)
funcloggingMiddleware(next mux.Handler) mux.Handler {
returnmux.HandlerFunc(func(w mux.ResponseWriter, r*mux.Message) {
log.Printf("ClientAddress %v, %v, %v\n", w.Client().RemoteAddr(), r.String(), r.Message.Code)
next.ServeCOAP(w, r)
})
}
funchandleG(w mux.ResponseWriter, r*mux.Message) {
err:=w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("/a")))
iferr!=nil {
log.Printf("cannot set response: %v", err)
}
}
funchandleA(w mux.ResponseWriter, r*mux.Message) {
ifr.Message.Code==codes.PUT {
log.Println("read the response from the server")
buf:=new(strings.Builder)
log.Println("created the reply buffer")
n, err:=io.Copy(buf, r.Body)
log.Println("done copying the reply body. n equals", n)
iferr!=nil&&n==0&&n!=0 {
log.Fatal("Is this here", err)
}
log.Printf("Payload of the Put request %v\n", buf.String())
} elseifr.Message.Code==codes.GET {
log.Printf("Responding to A GET request as usual")
}
err:=w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("hello world")))
iferr!=nil {
log.Printf("cannot set response: %v", err)
}
}
funchandleB(w mux.ResponseWriter, r*mux.Message) {
customResp:= message.Message{
Code: codes.Content,
Token: r.Token,
Context: r.Context,
Options: make(message.Options, 0, 16),
Body: bytes.NewReader([]byte("B hello world")),
}
optsBuf:=make([]byte, 32)
opts, used, err:=customResp.Options.SetContentFormat(optsBuf, message.TextPlain)
iferr==message.ErrTooSmall {
optsBuf=append(optsBuf, make([]byte, used)...)
opts, used, err=customResp.Options.SetContentFormat(optsBuf, message.TextPlain)
}
iferr!=nil {
log.Printf("cannot set options to response: %v", err)
return
}
optsBuf=optsBuf[:used]
customResp.Options=optserr=w.Client().WriteMessage(&customResp)
iferr!=nil {
log.Printf("cannot set response: %v", err)
}
}
funcmain() {
r:=mux.NewRouter()
r.Use(loggingMiddleware)
r.Handle("/.well-known/core", mux.HandlerFunc(handleG))
r.Handle("/a", mux.HandlerFunc(handleA))
r.Handle("/b", mux.HandlerFunc(handleB))
log.Fatal(coap.ListenAndServe("udp", ":5683", r))
for {
time.Sleep(5*time.Second)
r.HandleRemove("/a")
fmt.Println("Not giving service")
time.Sleep(5*time.Second)
r.Handle("/a", mux.HandlerFunc(handleA))
fmt.Println("giving sevice")
}
}
The text was updated successfully, but these errors were encountered:
Interesting. Im looking at solving the same issue.
I found this link that details examples of how to provide a proper /.well-known/core payload.
It seems like the payload should actually include information which I would consider implementation details of each endpoint.
Ideally this would be developed as a configurable middleware to go-coap, where you could provide details for each endpoint... but since the payload varies alot, I'm not sure its possible to provide a generic interface for creating a /.well-known/core payloads.
I am using a custom coap browser to discover what resouces are registered under a given coap server. In another word, list the paths available just like the one comes when we enter this on a coap browser bar:
coap.me/.well-known/core
But the simple server code you provided is not replying the list of url extension. I looked inside the code but I couldn't find any information. For now, I have managed to return one path when requested at
/.well-known/core
by changing the sever code like below. For next, could you tell me if the discovery feature I want is implemented in the library? if yes, please give me an example on how I can use it. In addition, I am checking if the libraryis it possible to change the registered paths on while the server is running?Thanks in advance
The text was updated successfully, but these errors were encountered: