Skip to content

Core\Http\Socket::sendBytes

$socket->sendBytes(bytes $frame): void$socket->sendBytes(
    bytes $frame,
): void

Sends one binary message to the server on a WebSocket connection your program opened.

A WebSocket message is either text or binary. send() sends text, and sendBytes() sends a bytes value as binary. The server receives all the bytes as one message. sendBytes() returns nothing. You may send tainted bytes, for example data you received earlier.

sendBytes() fails in the same ways as send(). It throws a TimeoutError if the server stops reading and the message cannot be written in time, and an IOError if the connection breaks. After close(), it throws a LogicError.

The examples below send four bytes, show how text and binary messages differ, and send sensor readings packed into six bytes each.

Parameters

  • $framebytesThe bytes to send, as one binary message. A tainted value is allowed, as it is for send.

Return value

Returns void. Nothing.

Errors

  • LogicErrorThe socket is closed, so the message cannot be sent.
  • TimeoutErrorThe frame was not written before sendTimeout ended. This usually means the peer has stopped reading. It is also thrown when the socket's maxDuration ends first.
  • IOErrorThe connection failed while the frame was going out.

Examples

Sending binary data

<?nvs
// Shows how to send binary data to a server over a WebSocket connection.
// A fixed server, so the example runs without a network. It sends nothing back.
Core\Test::answerSocket('wss://files.example.com/upload', []);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://files.example.com/upload');
// `sendBytes()` sends a `bytes` value as one binary message.
bytes $data = Core\Bytes::fill(4, 255);
$socket->sendBytes($data);
$socket->close();
// `Core\Test::sentSocket()` returns what the program sent. This prints `4 bytes: ffffffff`.
foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) {
tainted bytes $octets = $sent->bytes() ?? ('' as tainted bytes);
echo Core\Bytes::length($octets), ' bytes: ', Core\Encoding::toHex($octets), "\n";
}
Output
4 bytes: ffffffff

Text or binary

<?nvs
// Shows that `send()` sends a text message and `sendBytes()` sends a binary message.
// A fixed server, so the example runs without a network.
Core\Test::answerSocket('wss://chat.example.com/room', []);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://chat.example.com/room');
$socket->send('ok');
$socket->sendBytes('ok' as bytes);
// A text message has its content in `text()`, and a binary message has it in `bytes()`.
// This prints `text` for the first message and `binary` for the second.
foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) {
if ($sent->text() != null) {
echo "text\n";
} else {
echo "binary\n";
}
}
Output
text
binary

Sending sensor readings

<?nvs
// Shows a device that sends each sensor reading to a server as a small binary message.
// A fixed server, so the example runs without a network.
Core\Test::answerSocket('wss://sensors.example.com/readings', []);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://sensors.example.com/readings');
// Each reading is the sensor number in two bytes and the temperature in four bytes.
// `Core\Bytes::pack` writes the two numbers, so each message is six bytes long.
array<int> $temperatures = [21, 22, -3];
uint $sensor = 1;
foreach ($temperatures as int $temperature) {
$socket->sendBytes(Core\Bytes::pack('nN', $sensor, $temperature));
$sensor = $sensor + 1;
}
$socket->close();
// This prints each message as hexadecimal digits.
// The first line is `000100000015`: sensor 1 and a temperature of 21.
foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) {
echo Core\Encoding::toHex($sent->bytes() ?? ('' as tainted bytes)), "\n";
}
Output
000100000015
000200000016
0003fffffffd