当前位置: 首页 > 文档资料 > WorkerMan 3.x 手册 >

异步dns组件 - react/dns

优质
小牛编辑
127浏览
2023-12-01

(要求Workerman版本>=3.3.6)

安装:

  1. composer require react/dns

示例:

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use WorkermanWorker;
  4. $worker = new Worker('text://0.0.0.0:6161');
  5. $worker->onWorkerStart = function() {
  6. global $dns;
  7. $loop = Worker::getEventLoop();
  8. $factory = new ReactDnsResolverFactory();
  9. $dns = $factory->create('8.8.8.8', $loop);
  10. };
  11. $worker->onMessage = function($connection, $host) {
  12. global $dns;
  13. $dns->resolve($host)->then(function($ip) use($host, $connection) {
  14. $connection->send("$host: $ip");
  15. },function($e) use($host, $connection){
  16. $connection->send("$host: {$e->getMessage()}");
  17. });
  18. };
  19. Worker::runAll();

文档:

https://github.com/reactphp/dns

注意:

1、所有的异步编码必须在onXXX回调中编写

2、异步客户端需要的$loop变量请使用Worker::getEventLoop();返回值