Home > PHP Framework > Swoole > body text

What is the use of swoole client?

(*-*)浩
Release: 2019-12-07 10:33:49
Original
2544 people have browsed it

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();
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!