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

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

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";
    }
});
Last updated on August 31, 2022