Core\Http\Socket::send
Sends one text message to the server on a WebSocket connection your program opened.
The server receives the whole string as one message, however long it is. send() returns nothing. You may send a tainted value, for example a message you received earlier. To send binary data, use sendBytes().
send() waits until the message is written, but not forever. If the server stops reading and the message cannot be written in time, send() throws a TimeoutError. If the connection breaks, it throws an IOError. After close(), send() throws a LogicError, because nobody receives the message.
The examples below send two messages, show the error after a close, and send one JSON request for each product a program wants prices for.
Parameters
$framestringThe text to send, as one RFC 6455 text message, however many frames it needs. Ataintedvalue is allowed. Text your program sends to another server cannot run as code on your side.
Return value
Returns void. Nothing.
Errors
LogicErrorThe socket is closed, so the message cannot be sent.TimeoutErrorThe frame was not written beforesendTimeoutended. This usually means the peer has stopped reading. It is also thrown when the socket'smaxDurationends first.IOErrorThe connection failed while the frame was going out.
Examples
Sending a message
<?nvs// Shows how to send a text message 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://chat.example.com/room', []);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://chat.example.com/room');
// Each call to `send()` sends one message.$socket->send('hello');$socket->send('is anybody here?');$socket->close();
// `Core\Test::sentSocket()` returns what the program sent. This prints the two messages.foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) { echo 'sent: ', $sent->text() ?? '', "\n";}sent: hellosent: is anybody here?Sending after close
<?nvs// Shows what happens when a program sends a message on a connection that is already closed.
// 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('goodbye');$socket->close();
// After `close()`, nobody receives the message, so `send()` throws a `LogicError`.// This prints the error message.try { $socket->send('one more thing');} catch (LogicError $error) { echo 'error: ', $error->message, "\n";}
// Only the first message was sent. This prints `1 message sent`.echo Core\Arr::count(Core\Test::sentSocket()), " message sent\n";error: Core\Http\Socket::send(): this socket was closed, so there is nobody left to send to1 message sentSubscribing to a price feed
<?nvs// Shows how to ask a price server for updates by sending one JSON message per product.
// A fixed server, so the example runs without a network. It sends two prices and then closes.Core\Test::answerSocket('wss://prices.example.com/stream', ['{"symbol":"coffee","price":"4.20"}', '{"symbol":"tea","price":"3.10"}']);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://prices.example.com/stream');
// `Core\Json::encode` turns each request into JSON text, and `send()` sends it.array<string> $products = ['coffee', 'tea'];foreach ($products as string $product) { $socket->send(Core\Json::encode({ action: 'subscribe', symbol: $product }));}
// This prints the two prices the server sends back.?Core\Socket\Message $message = $socket->receive();while ($message != null) { echo 'update: ', $message->text() ?? '', "\n"; $message = $socket->receive();}
// This prints the two requests the program sent.foreach (Core\Test::sentSocket() as Core\Socket\Message $sent) { echo 'sent: ', $sent->text() ?? '', "\n";}update: {"symbol":"coffee","price":"4.20"}update: {"symbol":"tea","price":"3.10"}sent: {"action":"subscribe","symbol":"coffee"}sent: {"action":"subscribe","symbol":"tea"}