1.9.24 异步客户端自动解析域名
最后更新于:2022-04-02 06:17:41
# 1.9.24 异步客户端自动解析域名
[TOC]
在`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`
## 旧版本
~~~
swoole_async_dns_lookup("www.baidu.com", function ($domain, $ip) {
$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( $ip, 9501);
});
~~~
## 新版本
~~~
$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('www.baidu.com', 9501);
~~~
';