Closed robertsLando closed 4 months ago
Hi @robertsLando, thank you opening this issue! One approach to set the information for the response in the request handler could like this:
const coap = require("coap")
const server = coap.createServer()
server.on('request', (req, res) => {
const payload = "Hello World!"
res.code = "2.05"
res.end(payload)
})
server.listen(() => {
console.log('server started')
})
In node-coap, the token and message type are set automatically, so you don't need to touch those.
I think the code you've posted above got the handling of non-confirmable messages (the request_message_type == aiocoap.NON
part) a bit wrong, so this case doesn't really need to be handled separately, if I am not mistaken – with the default configuration, the server will just acknowledge all requests in so-called "piggybacked" response that both contains an acknowledgement and the actual response.
So to summarize, you should be able to keep this fairly simple :) I hope that I was able to help you out a bit, otherwise please me know and feel free to ask any question that you might have :)
@JKRhb Thanks a lot for your suggestion! I will try it out and let you know, closing for now 🙏🏼
I was looking for a way to convert this python script in NodeJS. I never used Coap protocol so based on a quick overview it seems to:
What leaves me with more questions is the way they send the response as I cannot find a way to set all those informations in the actual response:
How can I create the same response with your server?
Thanks in advance