Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | 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_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
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
.