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();
####reee#非阻塞客戶端,範例程式碼######
$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客戶端有什麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!