Swoole Server getClientInfo()

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Server->getClientInfo(int $fd, int $reactorId, bool $ignoreError = false): bool|array

Parameters

fd

The file descriptor number of the client connection you get info for

reactorId

The rector ID thread where the connection is located

ignoreError

Whether to ignore the fact that if a connection is closed, still return client information or only return information is the connection is still open, default is false which means only when a connection is still open

Return

success

If success, it returns an array, otherwise it returns false.

Description

Get client connection information, returns an array of information about a client for open or closed connections.

Things to Consider

  • The client certificate can only be obtained if the call is made within the callback of onConnect.
  • You will have to parse a client certificate using openssl_x509_parse
  • If using dispatch modes 1 or 3, take into consideration that these modes are using a stateless model, so related client information will be cleared from memory when the connection is broken or finishes so when calling this method in these modes, the data returned may be for another connection

Example

<?php

$info = $server->getClientInfo($fd);

var_dump($info);

// Array of data returned
array(5) {
  ["from_id"]=>
  int(3)
  ["server_fd"]=>
  int(14)
  ["server_port"]=>
  int(9501)
  ["remote_port"]=>
  int(19889)
  ["remote_ip"]=>
  string(9) "127.0.0.1"
  ["connect_time"]=>
  int(1390212495)
  ["last_time"]=>
  int(1390212760)
}

Data Information

  • reactor_id: Which Reactor thread the connection is from
  • server_fd: Which listening port socket the connection comes from, this is not the fd of the client connection, the server
  • server_port: The port of the server, the listening port used
  • remote_port: The port the client connects to
  • remote_ip: The IP address of the client connection
  • connect_time: The time the client connects to the Server, in seconds, set by the master process
  • last_time: The time of the last data received, in seconds, set by the master process
  • close_errno: The error code of the closed connection. If the connection is closed abnormally, the value of close_errno is non-zero. You can refer to the Linux error message list
  • recv_queued_bytes: The amount of data waiting to be processed
  • send_queued_bytes: The amount of data waiting to be sent
  • websocket_status: [Optional] WebSocket connection status, this information will be added when the server is a Swoole\WebSocket\Server
  • uid: [Optional] This information will be added when the user ID is bound with the server bind method
  • ssl_client_cert: [Optional] This information will be added when the client has set the certificate, only available when called from onConnect
Last updated on August 31, 2022