Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\WebSocket\Server->exist(int $fd): bool
The file descriptor of the WebSocket connection, it can be found from the frame object.
If success, it returns true
, otherwise it returns false
Check if the TCP connection is established, exists and is in an active state. This is a good way to check if the client is present. A connection only exists if it has completed the WebSocket handshake stage, if the connection is in the process of performing the handshake, this method will return false
.
Note: This method is only used to check if a TCP connection exists, you should use isEstablished()
to check specifically for a WebSocket connection type.
<?php
$server->on('Message', function Swoole\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";
}
});