OpenSwoole Server getClientList()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Server->getClientList(int $startFd = 0, int $pageSize = 10): bool|array

Parameters

startFd

The starting file descriptor number to begin from as clients connect based on a fd number that increases

pageSize

How many items are kept per page, the maximum shall not exceed 100

Return

success

If success, it returns a numeric array based on the starting $fd, otherwise it returns false. The array is sorted from smallest to largest

Description

Get a list of all the TCP connections for all the workers, based on a starting file descriptor, the default is 0 so that means to collect all information for all connected TCP clients.

This method is based shared memory operation, meaning it is fast and does not block, purely asynchronous.

Things to Consider

  • It is recommended to use the Server::$connections iterator to traverse these connections
  • This method is only available for TCP connections, not for UDP connections
  • When running the server in OpenSwoole\Server::SIMPLE_MODE mode, only the connection of the current process can obtained

Example

<?php

$start_fd = 0;

while(true)
{
    $conn_list = $server->getClientList($start_fd, 10);

    if($conn_list === false or count($conn_list) === 0)
    {
        echo "finish\n";
        break;
    }

    $start_fd = end($conn_list);
    var_dump($conn_list);

    foreach($conn_list as $fd)
    {
        $server->send($fd, "broadcast");
    }
}
Last updated on September 20, 2022