@@ -20,43 +20,31 @@ dependency.
2020Usage
2121-----
2222
23- The React client adapter needs a :ref: `message factory <message-factory >` in
24- order to to work::
25-
26- use Http\Adapter\React\Client;
27-
28- $client = new Client($messageFactory);
29-
30- For simplicity, all required objects are instantiated automatically if not
31- explicitly specified:
32-
33- :React\E ventLoop\L oopInterface: The event loop used inside the React engine.
34- :React\H ttpClient\C lient: The HTTP client instance that must be adapted.
35-
36- If you need more control on the React instances, you can inject them during
23+ If you need control on the React instances, you can inject them during
3724initialization::
3825
3926 use Http\Adapter\React\Client;
4027
41- $eventLoop = React\EventLoop\Factory::create();
42- $dnsResolverFactory = new React\Dns\Resolver\Factory();
43- $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
28+ $systemDnsConfig = React\Dns\Config\Config::loadSystemConfigBlocking();
29+ if (!$config->nameservers) {
30+ $config->nameservers[] = '8.8.8.8';
31+ }
4432
45- $factory = new React\HttpClient \Factory();
46- $reactHttp = $factory->create($loop, $dnsResolver );
33+ $dnsResolverFactory = new React\Dns\Resolver \Factory();
34+ $dnsResolver = $factory->create($config );
4735
48- $adapter = new Client($messageFactory, $eventLoop, $reactHttp);
36+ $connector = new React\Socket\Connector([
37+ 'dns' => $dnsResolver,
38+ ]);
39+ $browser = new React\Http\Browser($connector);
4940
50- If you choose to inject a custom React HTTP client, you must inject the loop
51- used during its construction. But if you already use an EventLoop inside your
52- code, you can inject only this object.
41+ $adapter = new Client($browser);
5342
5443You can also use a ``ReactFactory `` in order to initialize React instances::
5544
5645 use Http\Adapter\React\ReactFactory;
5746
58- $eventLoop = ReactFactory::buildEventLoop();
59- $reactHttp = ReactFactory::buildHttpClient($eventLoop);
47+ $reactHttp = ReactFactory::buildHttpClient();
6048
6149Then you can use the adapter to send synchronous requests::
6250
@@ -76,6 +64,18 @@ Or send asynchronous ones::
7664 // Returns a Http\Promise\Promise
7765 $promise = $adapter->sendAsyncRequest(request);
7866
67+ Note that since v4 calling `wait ` on `Http\Promise\Promise ` is expected to run inside a fiber::
68+
69+ use function React\Async\async;
70+
71+ async(static function () {
72+ // Returns a Http\Promise\Promise
73+ $promise = $adapter->sendAsyncRequest(request);
74+
75+ // Returns a Psr\Http\Message\ResponseInterface
76+ $response = $promise->wait();
77+ })();
78+
7979.. include :: includes/further-reading-async.inc
8080
8181.. _React HTTP client : https://github.com/reactphp/http-client
0 commit comments