-
-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for block transfer with UDP CoAP #593
Comments
Hi, Blockwise transfers are enabled by default for both DTLS and UDP. To use blockwise, simply send a Clientpackage main
import (
"bytes"
"context"
"log"
"os"
"time"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/udp"
)
func main() {
co, err := udp.Dial("localhost:5688")
if err != nil {
log.Fatalf("Error dialing: %v", err)
}
path := "/a"
if len(os.Args) > 1 {
path = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := co.Post(ctx, path, message.TextPlain, bytes.NewReader(make([]byte, 1024*1024)))
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
log.Printf("Response payload: %v", resp.String())
} Serverpackage main
import (
"bytes"
"io"
"log"
coap "github.com/plgd-dev/go-coap/v3"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/mux"
)
func loggingMiddleware(next mux.Handler) mux.Handler {
return mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
log.Printf("ClientAddress %v, %v\n", w.Conn().RemoteAddr(), r.String())
next.ServeCOAP(w, r)
})
}
func handleRequest(w mux.ResponseWriter, r *mux.Message) {
customResp := w.Conn().AcquireMessage(r.Context())
defer w.Conn().ReleaseMessage(customResp)
if r.Body() != nil {
data, err := io.ReadAll(r.Body())
if err != nil {
log.Printf("Cannot read body: %v", err)
return
}
log.Printf("Received length: %v", len(data))
}
customResp.SetCode(codes.Changed)
customResp.SetToken(r.Token())
customResp.SetContentFormat(message.TextPlain)
customResp.SetBody(bytes.NewReader([]byte("Response from server")))
err := w.Conn().WriteMessage(customResp)
if err != nil {
log.Printf("Cannot set response: %v", err)
}
}
func main() {
router := mux.NewRouter()
router.Use(loggingMiddleware)
router.Handle("/a", mux.HandlerFunc(handleRequest))
log.Fatal(coap.ListenAndServe("udp", ":5688", router))
} Additional ConfigurationTo customize blockwise behavior, use the option provided in WithBlockwise. Configure the server via |
Hi Jozef, thank you for your response, I will try it. I also figured it out today from the simple examples, it would be nice if we put our code to the examples, to help others as references. Client
Server
|
Hi,
Is there documentation or examples related to CoAP for block transfer for both client and server?
I searched throughout the examples and files and still did not find it. I would like to send a big chunk of payload around 1024 bytes from client to server. The simple example is the opposite, which is the client only GET from the server that serves some payload.
I also saw many issues asked related to block transfer examples. It would be helpful to tackle this problem together for the examples code.
The text was updated successfully, but these errors were encountered: