diff --git a/src/EventSource.php b/src/EventSource.php index 1269ba4..dbfd22e 100644 --- a/src/EventSource.php +++ b/src/EventSource.php @@ -211,13 +211,9 @@ private function request() $buffer = array_pop($messageEvents); foreach ($messageEvents as $data) { - $message = MessageEvent::parse($data, $this->lastEventId); + $message = MessageEvent::parse($data, $this->lastEventId, $this->reconnectTime); $this->lastEventId = $message->lastEventId; - if ($message->retry !== null) { - $this->reconnectTime = $message->retry / 1000; - } - if ($message->data !== '') { $this->emit($message->type, array($message)); if ($this->readyState === self::CLOSED) { diff --git a/src/MessageEvent.php b/src/MessageEvent.php index 35445e4..d1a5af5 100644 --- a/src/MessageEvent.php +++ b/src/MessageEvent.php @@ -7,10 +7,11 @@ class MessageEvent /** * @param string $data * @param string $lastEventId + * @param float $retryTime passed by reference, will be updated with `retry` field in seconds if valid * @return self * @internal */ - public static function parse($data, $lastEventId) + public static function parse($data, $lastEventId, &$retryTime = 0.0) { $lines = preg_split( '/\r\n|\r(?!\n)|\n/S', @@ -20,7 +21,6 @@ public static function parse($data, $lastEventId) $data = ''; $id = $lastEventId; $type = 'message'; - $retry = null; foreach ($lines as $line) { $name = strstr($line, ':', true); @@ -35,7 +35,7 @@ public static function parse($data, $lastEventId) } elseif ($name === 'event' && $value !== '') { $type = $value; } elseif ($name === 'retry' && $value === (string)(int)$value && $value >= 0) { - $retry = (int)$value; + $retryTime = $value * 0.001; } } @@ -43,7 +43,7 @@ public static function parse($data, $lastEventId) $data = substr($data, 0, -1); } - return new self($data, $id, $type, $retry); + return new self($data, $id, $type); } /** @@ -51,14 +51,12 @@ public static function parse($data, $lastEventId) * @param string $data * @param string $lastEventId * @param string $type - * @param ?int $retry */ - private function __construct($data, $lastEventId, $type, $retry) + private function __construct($data, $lastEventId, $type) { $this->data = $data; $this->lastEventId = $lastEventId; $this->type = $type; - $this->retry = $retry; } /** @@ -78,11 +76,4 @@ private function __construct($data, $lastEventId, $type, $retry) * @readonly */ public $type = 'message'; - - /** - * @internal - * @var ?int - * @readonly - */ - public $retry; } diff --git a/tests/MessageEventTest.php b/tests/MessageEventTest.php index 599c1ba..227c200 100644 --- a/tests/MessageEventTest.php +++ b/tests/MessageEventTest.php @@ -123,14 +123,14 @@ public function testParseWithEmptyEventReturnsMessageWithDefaultMessageType() public function retryTimeDataProvider() { return [ - ['retry: 1234', 1234,], - ['retry: 0', 0,], - ['retry: ' . PHP_INT_MAX, PHP_INT_MAX,], - ['retry: ' . PHP_INT_MAX . '9', null,], - ['retry: 1.234', null,], - ['retry: now', null,], - ['retry: -1', null,], - ['retry: -1.234', null,], + ['retry: 1234', 1.234], + ['retry: 0', 0.0], + ['retry: ' . PHP_INT_MAX, PHP_INT_MAX * 0.001], + ['retry: ' . PHP_INT_MAX . '9', null], + ['retry: 1.234', null], + ['retry: now', null], + ['retry: -1', null], + ['retry: -1.234', null] ]; } @@ -139,8 +139,9 @@ public function retryTimeDataProvider() */ public function testParseRetryTime($input, $expected) { - $message = MessageEvent::parse($input, ''); + $retryTime = null; + MessageEvent::parse($input, '', $retryTime); - $this->assertSame($expected, $message->retry); + $this->assertSame($expected, $retryTime); } }