Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?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 Swoole with enable-openssl
or with-openssl-dir
to support SSL. Also add SWOOLE_SSL
to the constructor.
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
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
.