1.9.24 异步客户端自动解析域名

1.9.24之前的版本,如果Client要通过域名连接服务器,需要手工调用swoole_async_dns_lookup函数,否则底层会发生阻塞。在最新的1.9.24中底层支持了自动异步解析域名,不再需要显式调用swoole_async_dns_lookup

有效范围

  • Swoole\Client
  • Swoole\Http\Client
  • Swoole\Coroutine\Client
  • Swoole\Coroutine\Http\Client

旧版本

  1. swoole_async_dns_lookup("www.baidu.com", function ($domain, $ip) {
  2. $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
  3. $client->on("connect", function(swoole_client $cli) {
  4. $cli->send("GET / HTTP/1.1\r\n\r\n");
  5. });
  6. $client->on("receive", function(swoole_client $cli, $data){
  7. echo "Receive: $data";
  8. $cli->send(str_repeat('A', 100)."\n");
  9. sleep(1);
  10. });
  11. $client->on("error", function(swoole_client $cli){
  12. echo "error\n";
  13. });
  14. $client->on("close", function(swoole_client $cli){
  15. echo "Connection close\n";
  16. });
  17. $client->connect( $ip, 9501);
  18. });

新版本

  1. $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
  2. $client->on("connect", function(swoole_client $cli) {
  3. $cli->send("GET / HTTP/1.1\r\n\r\n");
  4. });
  5. $client->on("receive", function(swoole_client $cli, $data){
  6. echo "Receive: $data";
  7. $cli->send(str_repeat('A', 100)."\n");
  8. sleep(1);
  9. });
  10. $client->on("error", function(swoole_client $cli){
  11. echo "error\n";
  12. });
  13. $client->on("close", function(swoole_client $cli){
  14. echo "Connection close\n";
  15. });
  16. //底层会自动进行异步域名解析
  17. $client->connect('www.baidu.com', 9501);