@@ -20,43 +20,31 @@ dependency.
20
20
Usage
21
21
-----
22
22
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
37
24
initialization::
38
25
39
26
use Http\Adapter\React\Client;
40
27
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
+ }
44
32
45
- $factory = new React\HttpClient \Factory();
46
- $reactHttp = $factory->create($loop, $dnsResolver );
33
+ $dnsResolverFactory = new React\Dns\Resolver \Factory();
34
+ $dnsResolver = $factory->create($config );
47
35
48
- $adapter = new Client($messageFactory, $eventLoop, $reactHttp);
36
+ $connector = new React\Socket\Connector([
37
+ 'dns' => $dnsResolver,
38
+ ]);
39
+ $browser = new React\Http\Browser($connector);
49
40
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);
53
42
54
43
You can also use a ``ReactFactory `` in order to initialize React instances::
55
44
56
45
use Http\Adapter\React\ReactFactory;
57
46
58
- $eventLoop = ReactFactory::buildEventLoop();
59
- $reactHttp = ReactFactory::buildHttpClient($eventLoop);
47
+ $reactHttp = ReactFactory::buildHttpClient();
60
48
61
49
Then you can use the adapter to send synchronous requests::
62
50
@@ -76,6 +64,18 @@ Or send asynchronous ones::
76
64
// Returns a Http\Promise\Promise
77
65
$promise = $adapter->sendAsyncRequest(request);
78
66
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
+
79
79
.. include :: includes/further-reading-async.inc
80
80
81
81
.. _React HTTP client : https://github.com/reactphp/http-client
0 commit comments