Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-26.11.1 | composer require openswoole/core:26.11.1
<?php OpenSwoole\WebSocket\Server->exists(int $fd): bool
The file descriptor of the WebSocket connection, it can be found from the frame object.
Returns true if the connection exists and is not closed, otherwise it returns false
Check if the TCP connection exists and is in an active state. This is a good way to check if the client is present. The method is inherited from OpenSwoole\Server->exists().
Note: This method only checks the TCP connection, it also returns true for a connection that has not completed the WebSocket handshake. Use isEstablished() to check specifically for an established WebSocket connection.
<?php
$server->on('Message', function (OpenSwoole\WebSocket\Server $server, $frame)
{
if($server->exists($frame->fd))
{
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "This is a message from the server.");
}
else
{
echo "Connection does not exist\n";
}
});