|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\EventSource; |
| 4 | + |
| 5 | +use React\EventLoop\LoopInterface; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
| 7 | +use React\Stream\ReadableStreamInterface; |
| 8 | +use Clue\React\Buzz\Browser; |
| 9 | +use Evenement\EventEmitter; |
| 10 | +use React\Socket\ConnectorInterface; |
| 11 | + |
| 12 | +class EventSource extends EventEmitter |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var string (read-only) last event ID received |
| 16 | + */ |
| 17 | + public $lastEventId = ''; |
| 18 | + |
| 19 | + // ready state |
| 20 | + const CONNECTING = 0; |
| 21 | + const OPEN = 1; |
| 22 | + const CLOSED = 2; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var int (read-only) |
| 26 | + * @see self::CONNECTING |
| 27 | + * @see self::OPEN |
| 28 | + * @see self::CLOSED |
| 29 | + */ |
| 30 | + public $readyState = self::CLOSED; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string (read-only) URL |
| 34 | + */ |
| 35 | + public $url; |
| 36 | + |
| 37 | + private $loop; |
| 38 | + private $browser; |
| 39 | + private $request; |
| 40 | + private $timer; |
| 41 | + private $reconnectTime = 3.0; |
| 42 | + |
| 43 | + public function __construct($url, LoopInterface $loop, ConnectorInterface $connector = null) |
| 44 | + { |
| 45 | + $parts = parse_url($url); |
| 46 | + if (!isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('http', 'https'))) { |
| 47 | + throw new \InvalidArgumentException(); |
| 48 | + } |
| 49 | + |
| 50 | + $browser = new Browser($loop, $connector); |
| 51 | + $this->browser = $browser->withOptions(array('streaming' => true, 'obeySuccessCode' => false)); |
| 52 | + $this->loop = $loop; |
| 53 | + $this->url = $url; |
| 54 | + |
| 55 | + $this->readyState = self::CONNECTING; |
| 56 | + |
| 57 | + $this->timer = $loop->addTimer(0, function () { |
| 58 | + $this->timer = null; |
| 59 | + $this->send(); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private function send() |
| 64 | + { |
| 65 | + $headers = array( |
| 66 | + 'Accept' => 'text/event-stream', |
| 67 | + 'Cache-Control' => 'no-cache' |
| 68 | + ); |
| 69 | + if ($this->lastEventId !== '') { |
| 70 | + $headers['Last-Event-ID'] = $this->lastEventId; |
| 71 | + } |
| 72 | + |
| 73 | + $this->request = $this->browser->get( |
| 74 | + $this->url, |
| 75 | + $headers |
| 76 | + ); |
| 77 | + $this->request->then(function (ResponseInterface $response) { |
| 78 | + if ($response->getStatusCode() !== 200) { |
| 79 | + $this->readyState = self::CLOSED; |
| 80 | + $this->emit('error', array(new \UnexpectedValueException('Unexpected status code'))); |
| 81 | + $this->close(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if ($response->getHeaderLine('Content-Type') !== 'text/event-stream') { |
| 86 | + $this->readyState = self::CLOSED; |
| 87 | + $this->emit('error', array(new \UnexpectedValueException('Unexpected Content-Type'))); |
| 88 | + $this->close(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $stream = $response->getBody(); |
| 93 | + assert($stream instanceof ReadableStreamInterface); |
| 94 | + |
| 95 | + $buffer = ''; |
| 96 | + $stream->on('data', function ($chunk) use (&$buffer, $stream) { |
| 97 | + $buffer .= $chunk; |
| 98 | + |
| 99 | + while (($pos = strpos($buffer, "\n\n")) !== false) { |
| 100 | + $data = substr($buffer, 0, $pos); |
| 101 | + $buffer = substr($buffer, $pos + 2); |
| 102 | + |
| 103 | + $message = MessageEvent::parse($data); |
| 104 | + if ($message->lastEventId === null) { |
| 105 | + $message->lastEventId = $this->lastEventId; |
| 106 | + } else { |
| 107 | + $this->lastEventId = $message->lastEventId; |
| 108 | + } |
| 109 | + |
| 110 | + if ($message->data !== '') { |
| 111 | + $this->emit($message->type, array($message)); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + $stream->on('close', function () { |
| 117 | + $this->request = null; |
| 118 | + if ($this->readyState === self::OPEN) { |
| 119 | + $this->readyState = self::CONNECTING; |
| 120 | + $this->timer = $this->loop->addTimer($this->reconnectTime, function () { |
| 121 | + $this->timer = null; |
| 122 | + $this->send(); |
| 123 | + }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + $this->readyState = self::OPEN; |
| 128 | + $this->emit('open'); |
| 129 | + })->then(null, function ($e) { |
| 130 | + $this->request = null; |
| 131 | + if ($this->readyState === self::CLOSED) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + $this->emit('error', array($e)); |
| 136 | + if ($this->readyState === self::CLOSED) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + $this->timer = $this->loop->addTimer($this->reconnectTime, function () { |
| 141 | + $this->timer = null; |
| 142 | + $this->send(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + public function close() |
| 148 | + { |
| 149 | + $this->readyState = self::CLOSED; |
| 150 | + if ($this->request !== null) { |
| 151 | + $request = $this->request; |
| 152 | + $this->request = null; |
| 153 | + |
| 154 | + $request->then(function (ResponseInterface $response) { |
| 155 | + $response->getBody()->close(); |
| 156 | + }); |
| 157 | + $request->cancel(); |
| 158 | + } |
| 159 | + |
| 160 | + if ($this->timer !== null) { |
| 161 | + $this->loop->cancelTimer($this->timer); |
| 162 | + $this->timer = null; |
| 163 | + } |
| 164 | + |
| 165 | + $this->removeAllListeners(); |
| 166 | + } |
| 167 | +} |
0 commit comments