Swoole\Coroutine\Client::__construct

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php public function __construct($clientType)

Parameters

clientType

The type of the client to create, see constants below.

Return

Swoole\Coroutine\Client

A new TCP/UDP client instance.

Description

Create a coroutine TCP/UDP client within a coroutine context, with or without SSL.

Compile Swoole with enable-openssl or with-openssl-dir to support SSL. Also add SWOOLE_SSL to the constructor.

Client Types

You can use the following client types when creating a new TCP/UDP client.

  • SWOOLE_SOCK_TCP
  • SWOOLE_SOCK_TCP6
  • SWOOLE_SOCK_UDP
  • SWOOLE_SOCK_UDP6
  • SWOOLE_SOCK_UNIX_DGRAM
  • SWOOLE_SOCK_UNIX_STREAM
  • SWOOLE_SSL
  • SWOOLE_KEEP

Example

To establish an SSL connection.

<?php
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP | SWOOLE_SSL);

...

To create persistent TCP connection:

<?php
$client = new Swoole\Coroutine\Client(SWOOLE_TCP | SWOOLE_KEEP);

...

A persistent connection created by using SWOOLE_KEEP will not be closed, even when calling $client->close(). This makes it easier when having to reconnect again after the server disconnects or an error happens, the connection details are saved. The client deconstructor will handle any connection closing when using SWOOLE_KEEP.

Last updated on August 31, 2022