Swoole\WebSocket\Server->exist(...)

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

Declaration

<?php
Swoole\WebSocket\Server->exist(int $fd): bool

Parameters

fd

The file descriptor of the WebSocket connection, it can be found from the frame object.

Return

If success, it returns true, otherwise it returns false

Description

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.

Example

<?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";
    }
});