|
4 | 4 |
|
5 | 5 | use Evenement\EventEmitter; |
6 | 6 | use Psr\Http\Message\ResponseInterface; |
| 7 | +use React\EventLoop\Loop; |
7 | 8 | use React\EventLoop\LoopInterface; |
8 | 9 | use React\Http\Browser; |
9 | 10 | use React\Stream\ReadableStreamInterface; |
|
15 | 16 | * web browsers. Unless otherwise noted, it follows the same semantics as defined |
16 | 17 | * under https://html.spec.whatwg.org/multipage/server-sent-events.html |
17 | 18 | * |
18 | | - * It requires the URL to the remote Server-Sent Events (SSE) endpoint and also |
19 | | - * registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage) |
20 | | - * in order to handle async HTTP requests. |
| 19 | + * Its constructor simply requires the URL to the remote Server-Sent Events (SSE) endpoint: |
21 | 20 | * |
22 | 21 | * ```php |
23 | | - * $loop = React\EventLoop\Factory::create(); |
24 | | - * |
25 | | - * $es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', $loop); |
| 22 | + * $es = new Clue\React\EventSource\EventSource('https://example.com/stream.php'); |
26 | 23 | * ``` |
27 | 24 | * |
| 25 | + * This class takes an optional `LoopInterface|null $loop` parameter that can be used to |
| 26 | + * pass the event loop instance to use for this object. You can use a `null` value |
| 27 | + * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). |
| 28 | + * This value SHOULD NOT be given unless you're sure you want to explicitly use a |
| 29 | + * given event loop instance. |
| 30 | + * |
28 | 31 | * If you need custom connector settings (DNS resolution, TLS parameters, timeouts, |
29 | 32 | * proxy servers etc.), you can explicitly pass a custom instance of the |
30 | 33 | * [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface) |
31 | 34 | * to the [`Browser`](https://github.com/reactphp/http#browser) instance |
32 | 35 | * and pass it as an additional argument to the `EventSource` like this: |
33 | 36 | * |
34 | 37 | * ```php |
35 | | - * $connector = new React\Socket\Connector($loop, array( |
| 38 | + * $connector = new React\Socket\Connector(null, [ |
36 | 39 | * 'dns' => '127.0.0.1', |
37 | | - * 'tcp' => array( |
| 40 | + * 'tcp' => [ |
38 | 41 | * 'bindto' => '192.168.10.1:0' |
39 | | - * ), |
40 | | - * 'tls' => array( |
| 42 | + * ], |
| 43 | + * 'tls' => [ |
41 | 44 | * 'verify_peer' => false, |
42 | 45 | * 'verify_peer_name' => false |
43 | | - * ) |
44 | | - * )); |
45 | | - * $browser = new React\Http\Browser($loop, $connector); |
| 46 | + * ] |
| 47 | + * ]); |
| 48 | + * $browser = new React\Http\Browser(null, $connector); |
46 | 49 | * |
47 | | - * $es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', $loop, $browser); |
| 50 | + * $es = new Clue\React\EventSource\EventSource('https://example.com/stream.php', null, $browser); |
48 | 51 | * ``` |
49 | 52 | */ |
50 | 53 | class EventSource extends EventEmitter |
@@ -78,18 +81,24 @@ class EventSource extends EventEmitter |
78 | 81 | private $timer; |
79 | 82 | private $reconnectTime = 3.0; |
80 | 83 |
|
81 | | - public function __construct($url, LoopInterface $loop, Browser $browser = null) |
| 84 | + /** |
| 85 | + * @param string $url |
| 86 | + * @param ?LoopInterface $loop |
| 87 | + * @param ?Browser $browser |
| 88 | + * @throws \InvalidArgumentException for invalid URL |
| 89 | + */ |
| 90 | + public function __construct($url, LoopInterface $loop = null, Browser $browser = null) |
82 | 91 | { |
83 | 92 | $parts = parse_url($url); |
84 | 93 | if (!isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('http', 'https'))) { |
85 | 94 | throw new \InvalidArgumentException(); |
86 | 95 | } |
87 | 96 |
|
| 97 | + $this->loop = $loop ?: Loop::get(); |
88 | 98 | if ($browser === null) { |
89 | | - $browser = new Browser($loop); |
| 99 | + $browser = new Browser($this->loop); |
90 | 100 | } |
91 | 101 | $this->browser = $browser->withRejectErrorResponse(false); |
92 | | - $this->loop = $loop; |
93 | 102 | $this->url = $url; |
94 | 103 |
|
95 | 104 | $this->readyState = self::CONNECTING; |
|
0 commit comments