多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# TCP客户端异步连接 通过使用on方法注册异步回调函数。多个swoole\_client可以嵌套回调。异步模式仅可用于cli模式,如在swoole\_process或swoole\_server中。 示例: ~~~ <?php $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞 $client->on("connect", function($cli) { echo "connected\n"; $cli->send("hello world\n"); }); $client->on("receive", function($cli, $data) { if(empty($data)){ $cli->close(); echo "closed\n"; } else { echo "received: $data\n"; sleep(1); $cli->send("hello\n"); } }); $client->on("error", function($cli){ exit("error\n"); }); $client->on("close", function($cli){ echo "connection is closed\n"; }); $client->connect('127.0.0.1', 9501, 0.5); ~~~