Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/EventSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 5 additions & 14 deletions src/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -35,30 +35,28 @@ 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;
}
}

if (substr($data, -1) === "\n") {
$data = substr($data, 0, -1);
}

return new self($data, $id, $type, $retry);
return new self($data, $id, $type);
}

/**
* @internal
* @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;
}

/**
Expand All @@ -78,11 +76,4 @@ private function __construct($data, $lastEventId, $type, $retry)
* @readonly
*/
public $type = 'message';

/**
* @internal
* @var ?int
* @readonly
*/
public $retry;
}
21 changes: 11 additions & 10 deletions tests/MessageEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
];
}

Expand All @@ -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);
}
}