Skip to content

Core\Http\Socket::close

$socket->close(?uint $code = null, ?string $reason = null): void$socket->close(
    ?uint $code = null,
    ?string $reason = null,
): void

Ends a WebSocket connection that your program opened.

close() sends a close message to the server and waits a short time for the server to answer. Then the connection is gone. After a close, receive() returns null, and send() throws a LogicError. Calling close() a second time does nothing.

You can give a close code and a reason. The code is a number that says why the connection ended. If you leave it out, it is 1000, which means a normal ending. The reason is a short text the server may write to its log. A server that never answers the close does not cause an error.

Good to know: a connection is also closed when the request or task that opened it ends. Call close() when you are finished earlier, so the server stops sending.

Parameters

  • $code?uintdefault nullThe close code to send. If you leave it out, it is 1000, a normal close.
  • $reason?stringdefault nullText to send with the code, for a peer that logs it. If you leave it out, no text is sent.

Return value

Returns void. Nothing. If the peer never sends its close frame, the connection is closed anyway, without an error. So a normal close needs no catch. Closing a socket that is already closed does nothing.

Examples

Closing when you are done

<?nvs
// Shows how to close a WebSocket connection when your program has what it needs.
// A fixed server, so the example runs without a network. It has three messages to send.
Core\Test::answerSocket('wss://chat.example.com/room', ['hello', 'how are you?', 'bye']);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://chat.example.com/room');
// This prints `hello`.
echo $socket->receive()?->text() ?? 'nothing', "\n";
// `close()` ends the connection. After it, `receive()` returns `null`.
// The two messages the server did not send yet are never read.
$socket->close();
if ($socket->receive() == null) {
// This prints `closed`.
echo "closed\n";
}
Output
hello
closed

A close code and a reason

<?nvs
// Shows how to close a connection with a close code and a reason,
// and what happens when a program sends a message after that.
// A fixed server, so the example runs without a network.
Core\Test::answerSocket('wss://game.example.com/match', ['start']);
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://game.example.com/match');
$socket->send('ready');
// The close code `1001` means "going away". The reason is a short text the server may log.
$socket->close(1001, 'the player left the game');
// A closed connection cannot send. `send()` throws a `LogicError`.
try {
$socket->send('one more move');
} catch (LogicError $error) {
echo $error->message, "\n";
}
// Calling `close()` again does nothing. This prints `closed twice`.
$socket->close();
echo "closed twice\n";
Output
Core\Http\Socket::send(): this socket was closed, so there is nobody left to send to
closed twice

Closing after an error

<?nvs
// Shows a program that reads orders from a live feed. It closes the connection
// in a `finally` block, so the connection is closed even when a step fails.
class OrderFeed
{
// Prints each order until the feed sends `stop`. An empty order throws an error.
public static function follow(string $url): void
{
Core\Http\Socket $socket = Core\Http\Client::openSocket($url);
try {
while (true) {
?Core\Socket\Message $message = $socket->receive();
if ($message == null) {
return;
}
tainted string $order = $message->text() ?? ('' as tainted string);
if ($order == 'stop') {
return;
}
if ($order == '') {
throw new RuntimeError('the feed sent an empty order');
}
echo 'order: ', $order, "\n";
}
} finally {
// This runs after `return` and after an error.
$socket->close();
echo "connection closed\n";
}
}
}
// Two fixed feeds, so the example runs without a network.
Core\Test::answerSocket('wss://orders.example.com/good', ['A-17', 'B-4', 'stop', 'C-9']);
Core\Test::answerSocket('wss://orders.example.com/broken', ['A-18', '']);
// The first feed stops normally. The second feed fails, and the connection is still closed.
OrderFeed::follow('wss://orders.example.com/good');
try {
OrderFeed::follow('wss://orders.example.com/broken');
} catch (RuntimeError $error) {
echo 'error: ', $error->message, "\n";
}
Output
order: A-17
order: B-4
connection closed
order: A-18
connection closed
error: the feed sent an empty order