Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-25.2.0 | composer require openswoole/core:22.1.5
<?php public function __construct($clientType)
The type of the client to create, see constants below.
A new TCP/UDP client instance.
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.
You can use the following client types when creating a new TCP/UDP client.
OpenSwoole\Constant::SOCK_TCPOpenSwoole\Constant::SOCK_TCP6OpenSwoole\Constant::SOCK_UDPOpenSwoole\Constant::SOCK_UDP6OpenSwoole\Constant::SOCK_UNIX_DGRAMOpenSwoole\Constant::SOCK_UNIX_STREAMOpenSwoole\Constant::SSLOpenSwoole\Constant::KEEPTo 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.