Core\Net\Datagram\Message::payload
Returns the bytes of a message that Core\Net\Datagram::receive returned. The result is tainted bytes, because the bytes come from the network. Check them before you use them in a query, a file name or a web page.
An empty result is a real message, because a UDP message can have zero bytes. If the message was longer than the $max you gave receive, the result has only the first $max bytes.
You can call payload() as often as you want. It returns the same bytes each time, also after the socket is closed.
Return value
Returns tainted bytes. The bytes that arrived. They can be empty, because a message with no bytes is still a message.
Examples
The examples below run with this configuration file.
# The examples open UDP sockets on this computer and send messages between them.# These lines allow a socket only at `127.0.0.1`, on a port the system picks.# A program run with `nvs run` may send to this computer without a grant.[capabilities.net]listen = ["127.0.0.1:0"]Reading the bytes
<?nvs// Shows how to read the bytes of a UDP message you received.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);Core\Net\Datagram $client = Core\Net::bindDatagram('127.0.0.1', 0);$client->send('127.0.0.1', $server->port(), 'hello' as bytes, 1s);Core\Net\Datagram\Message $message = $server->receive(64, 5s);
// `payload()` returns `tainted bytes`. `as string` converts them to text.// This prints `payload: hello`.tainted string $text = $message->payload() as string;echo 'payload: ', $text, "\n";
$client->close();$server->close();payload: helloAn empty message
<?nvs// Shows that a UDP message can be empty, and that an empty payload is still a message.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);Core\Net\Datagram $client = Core\Net::bindDatagram('127.0.0.1', 0);
// `send` returns 0, because the message has no bytes.echo 'sent: ', $client->send('127.0.0.1', $server->port(), '' as bytes, 1s), "\n";
// `receive` returns a message, and its payload is empty. This prints `empty: yes`.Core\Net\Datagram\Message $message = $server->receive(64, 5s);echo 'empty: ', $message->payload() as string == '' ? 'yes' : 'no', "\n";
$client->close();$server->close();sent: 0empty: yesA metrics collector
<?nvs// Shows a small metrics collector that adds up the counters that programs send to it.
Core\Net\Datagram $collector = Core\Net::bindDatagram('127.0.0.1', 0);Core\Net\Datagram $app = Core\Net::bindDatagram('127.0.0.1', 0);
// Each message is one line such as `hits:3`: a name, a colon and a number.array<string> $lines = ['hits:3', 'hits:4', 'bad line'];foreach ($lines as string $line) { $app->send('127.0.0.1', $collector->port(), $line as bytes, 1s);}
// The collector splits each payload at the colon. A line without a number is skipped.int $hits = 0;uint $i = 0;while ($i < 3) { tainted string $text = $collector->receive(512, 5s)->payload() as string; array<tainted string> $parts = Core\Str::split($text, ':'); // `as ?int` converts the text to a number. If that is not possible, the result is `null`. ?int $count = Core\Arr::count($parts) == 2 ? $parts[1] as ?int : null; if ($count != null) { $hits = $hits + $count; } $i = $i + 1;}// This prints `hits: 7`.echo 'hits: ', $hits, "\n";
$app->close();$collector->close();hits: 7