OpenSwoole\Coroutine\Client::__construct

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

Declaration

<?php public function __construct($clientType)

Parameters

clientType

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

Return

OpenSwoole\Coroutine\Client

A new TCP/UDP client instance.

Description

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

Compile OpenSwoole with enable-openssl or with-openssl-dir to support SSL. Also add OpenSwoole\Constant::SSL to the constructor.

Client Types

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

  • OpenSwoole\Constant::SOCK_TCP
  • OpenSwoole\Constant::SOCK_TCP6
  • OpenSwoole\Constant::SOCK_UDP
  • OpenSwoole\Constant::SOCK_UDP6
  • OpenSwoole\Constant::SOCK_UNIX_DGRAM
  • OpenSwoole\Constant::SOCK_UNIX_STREAM
  • OpenSwoole\Constant::SSL
  • OpenSwoole\Constant::KEEP

Example

To establish an SSL connection.

<?php
$client = new OpenSwoole\Coroutine\Client(OpenSwoole\Constant::SOCK_TCP | OpenSwoole\Constant::SSL);

...

To create persistent TCP connection:

<?php
$client = new OpenSwoole\Coroutine\Client(OpenSwoole\Constant::TCP | OpenSwoole\Constant::KEEP);

...

A persistent connection created by using OpenSwoole\Constant::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 OpenSwoole\Constant::KEEP.

Last updated on September 20, 2022