Home >PHP Framework >Swoole >What is the use of swoole client?

What is the use of swoole client?

(*-*)浩
(*-*)浩Original
2019-12-07 10:33:492621browse

What is the use of swoole client?

client client

Client provides the encapsulation code of the TCP/UDP socket client, which only requires new Swoole\ Client is enough.

In addition to the ordinary synchronous blocking select method, Client also supports asynchronous non-blocking callbacks.                    (Recommended learning: swoole video tutorial)

Synchronously blocking the client, sample code

$client = new swoole_client(SWOOLE_SOCK_TCP);

if (!$client->connect('127.0.0.1', 9501, -1))
{
    exit("connect failed. Error: {$client->errCode}\n");
}

$client->send("hello world\n");

echo $client->recv();

$client->close();

Asynchronous Non-blocking client, sample code

$client = new Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) {
    $cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function(swoole_client $cli, $data){
    echo "Receive: $data";
    $cli->send(str_repeat('A', 100)."\n");
    sleep(1);
});
$client->on("error", function(swoole_client $cli){
    echo "error\n";
});
$client->on("close", function(swoole_client $cli){
    echo "Connection close\n";
});
$client->connect('127.0.0.1', 9501);

The above is the detailed content of What is the use of swoole client?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn