Core\Socket\Message::bytes
Returns the data of a message that the client sent as binary.
A WebSocket client can send two kinds of message: text and binary. bytes() returns the data of a binary message as bytes, exactly as the client sent it. For a text message it returns null, and text() returns the text instead. It also returns null for a message that came from a topic.
The data is tainted, because it comes from the client and a client can send anything. A binary message can be empty. Then bytes() returns empty bytes, not null.
The examples below read the size of a binary message, tell a text message from a binary message, and receive a file that the client sends in several pieces.
Return value
Returns ?tainted bytes. The bytes, exactly as the client sent them. They are tainted, because they come from the client. It is null for a text message and for a message from a topic.
Examples
Reading binary data
<?nvs// Shows how to read the data of a binary message.
// The `.nvsp` file beside this example plays the client.// It sends one binary message of five bytes and then closes the connection.Core\Socket $conn = Core\Socket::current();
// `bytes()` returns the data exactly as the client sent it.// This prints `5 bytes`.?Core\Socket\Message $message = $conn->receive();if ($message != null) { tainted bytes $data = $message->bytes() ?? '' as bytes; echo Core\Bytes::length($data), " bytes\n";}5 bytesA text message has no bytes
<?nvs// Shows that `bytes()` returns `null` for a text message.
// The `.nvsp` file beside this example plays the client.// It sends a binary message, then a text message.Core\Socket $conn = Core\Socket::current();
// This prints `binary: 2 bytes`, then `no bytes: this is a text message`.?Core\Socket\Message $message = $conn->receive();while ($message != null) { ?tainted bytes $data = $message->bytes(); if ($data == null) { echo "no bytes: this is a text message\n"; } else { echo 'binary: ', Core\Bytes::length($data), " bytes\n"; } $message = $conn->receive();}binary: 2 bytesno bytes: this is a text messageReceiving a file in pieces
<?nvs// Shows a connection that receives a PDF file from the client in several binary messages.
// The `.nvsp` file beside this example plays the client.// It sends a file in three pieces and then closes the connection.Core\Socket $conn = Core\Socket::current();
// This adds up the size of every piece and sends the total to the client.// This prints `sent: received 3 pieces, 16 bytes`.uint $pieces = 0;uint $size = 0;?Core\Socket\Message $message = $conn->receive();while ($message != null) { tainted bytes $piece = $message->bytes() ?? '' as bytes; // A PDF file starts with the four bytes `%PDF`. The first piece must start with them. if ($pieces == 0 && !Core\Bytes::startsWith($piece, '%PDF' as bytes)) { $conn->send('this is not a PDF file'); break; } $pieces = $pieces + 1; $size = $size + Core\Bytes::length($piece); $message = $conn->receive();}if ($pieces > 0) { $conn->send('received ' . $pieces . ' pieces, ' . $size . ' bytes');}sent: received 3 pieces, 16 bytes