时间:2021-07-01 10:21:17 帮助过:147人阅读
client客户端
Client提供了TCP/UDP socket的客户端的封装代码,使用时仅需 new Swoole\Client 即可。
除了普通的同步阻塞+select的使用方法外,Client还支持异步非阻塞回调。 (推荐学习: swoole视频教程)
同步阻塞客户端,示例代码
$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();
异步非阻塞客户端,示例代码
$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);
以上就是swoole客户端有什么用的详细内容,更多请关注Gxlcms其它相关文章!