udp.handle()
udp.handle(port, handler, encrypt)
Listens for UDP packets on a port and passes them to a callback — only one listener is supported.
Registers the device's single UDP listener. The handler receives a request
object whose req.data holds the packet payload — parse JSON payloads with
JSON.parse(). When encrypt is true, payloads are decrypted with AES-256 and the integrity validated with a SHA256 checksum before the handler runs.
Arguments
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| port | number | yes | — | UDP port to listen on, e.g. 9000. |
| handler | function | yes | — | Callback function(req){ … } — req.data is the received payload. |
| encrypt | boolean | yes | — | true to expect AES-256 encrypted payloads. |
Example
udp.handle(9000, function(req){ // only a single udp.handle listener is supported
//req = {port:4000,host:"172.16.220.1",data:"{msg:\"Hello World!\"}"}
let data = JSON.parse(req.data); // parse the received payload as JSON
print(data.msg);
}, false); // false: do not use AES-256 encryption or checksums