|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\HttpClient; |
| 4 | + |
| 5 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 6 | +use Symfony\Component\HttpClient\Exception\ClientException; |
| 7 | +use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 8 | + |
| 9 | +final class EventSource extends EventDispatcher |
| 10 | +{ |
| 11 | + // ready state |
| 12 | + const CONNECTING = 0; |
| 13 | + const OPEN = 1; |
| 14 | + const CLOSED = 2; |
| 15 | + |
| 16 | + // events |
| 17 | + const ON_MESSAGE = 'eventsource:onmessage'; |
| 18 | + const ON_OPEN = 'eventsource:onopen'; |
| 19 | + const ON_ERROR = 'eventsource:onerror'; |
| 20 | + |
| 21 | + public $lastEventId = ''; |
| 22 | + public $readyState = self::CLOSED; |
| 23 | + |
| 24 | + private $client; |
| 25 | + private $reconnectionTime = 30; |
| 26 | + private $shouldDisconnect = false; |
| 27 | + |
| 28 | + public function __construct(HttpClientInterface $client = null) |
| 29 | + { |
| 30 | + $this->client = $client ?: HttpClient::create($configuration['default_options'] ?? []); |
| 31 | + } |
| 32 | + |
| 33 | + public function connect(string $url) |
| 34 | + { |
| 35 | + $parts = parse_url($url); |
| 36 | + if (!isset($parts['scheme'], $parts['host']) || !\in_array($parts['scheme'], ['http', 'https'])) { |
| 37 | + throw new InvalidArgumentException(); |
| 38 | + } |
| 39 | + $this->loop($url); |
| 40 | + } |
| 41 | + |
| 42 | + public function disconnect() |
| 43 | + { |
| 44 | + $this->shouldDisconnect = true; |
| 45 | + } |
| 46 | + |
| 47 | + private function doRequest(string $url) |
| 48 | + { |
| 49 | + $headers = [ |
| 50 | + 'Accept' => 'text/event-stream', |
| 51 | + 'Cache-Control' => 'no-cache', |
| 52 | + ]; |
| 53 | + |
| 54 | + if ('' !== $this->lastEventId) { |
| 55 | + $headers['Last-Event-ID'] = $this->lastEventId; |
| 56 | + } |
| 57 | + |
| 58 | + $this->readyState = self::CONNECTING; |
| 59 | + |
| 60 | + return $this->client->request('GET', $url, ['headers' => $headers]); |
| 61 | + } |
| 62 | + |
| 63 | + private function loop(string $url) |
| 64 | + { |
| 65 | + $response = $this->doRequest($url); |
| 66 | + |
| 67 | + if (200 !== $response->getStatusCode()) { |
| 68 | + throw new ClientException($response); |
| 69 | + $this->readyState = self::CLOSED; |
| 70 | + $response->cancel(); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $this->readyState = self::OPEN; |
| 76 | + $this->dispatch(new MessageEvent(), self::ON_OPEN); |
| 77 | + |
| 78 | + $buffer = ''; |
| 79 | + foreach ($this->client->stream($response, $this->reconnectionTime) as $chunk) { |
| 80 | + if ($this->shouldDisconnect || $chunk->isTimeout()) { |
| 81 | + $this->readyState = self::CLOSED; |
| 82 | + if (false === $this->shouldDisconnect) { |
| 83 | + $this->loop($url); |
| 84 | + } |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // We have to gather our own Buffer that may be longer then a chunk |
| 90 | + $buffer .= $chunk->getContent(); |
| 91 | + |
| 92 | + if (!$buffer) { |
| 93 | + // connection closed |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // If the line starts with a U+003A COLON character (':') ignore the line |
| 98 | + if (':' === $buffer[0]) { |
| 99 | + $buffer = ''; |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + // Process if we got new line ending |
| 104 | + $end = substr($buffer, -1); |
| 105 | + if ("\n" !== $end || "\r" !== $end) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $messageEvent = MessageEvent::parse($buffer); |
| 110 | + $this->lastEventId = $messageEvent->lastEventId; |
| 111 | + $buffer = ''; |
| 112 | + |
| 113 | + if ('retry' === $messageEvent->eventName && \is_int($time = (int) $messageEvent->data)) { |
| 114 | + $this->reconnectionTime = $time; |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + $this->dispatch($messageEvent, self::ON_MESSAGE); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments