Skip to content

Commit 44138a8

Browse files
committed
Added support for the "retry" field.
1 parent 09bc48a commit 44138a8

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/EventSource.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ private function request()
195195
$this->lastEventId = $message->lastEventId;
196196
}
197197

198+
if ($message->retry !== null) {
199+
$this->reconnectTime = $message->retry / 1000;
200+
}
201+
198202
if ($message->data !== '') {
199203
$this->emit($message->type, array($message));
200204
if ($this->readyState === self::CLOSED) {

src/MessageEvent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static function parse($data)
2929
$message->lastEventId .= $value;
3030
} elseif ($name === 'event') {
3131
$message->type = $value;
32+
} elseif ($name === 'retry') {
33+
$message->retry = (int)$value;
3234
}
3335
}
3436

@@ -53,4 +55,9 @@ public static function parse($data)
5355
* @var string
5456
*/
5557
public $type = 'message';
58+
59+
/**
60+
* @var ?int
61+
*/
62+
public $retry;
5663
}

tests/EventSourceTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,42 @@ public function testReconnectAfterStreamClosesUsesLastEventIdFromParsedEventStre
583583
$timerReconnect();
584584
}
585585

586+
public function testReconnectAfterStreamClosesUsesSpecifiedRetryTime()
587+
{
588+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
589+
$timerReconnect = null;
590+
$loop->expects($this->once())->method('addTimer')->with(
591+
2.543,
592+
$this->callback(function ($cb) use (&$timerReconnect) {
593+
$timerReconnect = $cb;
594+
return true;
595+
})
596+
);
597+
598+
$deferred = new Deferred();
599+
$browser = $this->getMockBuilder('React\Http\Browser')->disableOriginalConstructor()->getMock();
600+
$browser->expects($this->once())->method('withRejectErrorResponse')->willReturnSelf();
601+
$browser->expects($this->exactly(2))->method('requestStreaming')->withConsecutive(
602+
['GET', 'http://example.com', ['Accept' => 'text/event-stream', 'Cache-Control' => 'no-cache']],
603+
['GET', 'http://example.com', ['Accept' => 'text/event-stream', 'Cache-Control' => 'no-cache']]
604+
)->willReturnOnConsecutiveCalls(
605+
$deferred->promise(),
606+
new Promise(function () { })
607+
);
608+
609+
$es = new EventSource('http://example.com', $browser, $loop);
610+
611+
$stream = new ThroughStream();
612+
$response = new Response(200, array('Content-Type' => 'text/event-stream'), new ReadableBodyStream($stream));
613+
$deferred->resolve($response);
614+
615+
$stream->write("retry:2543\n\n");
616+
$stream->end();
617+
618+
$this->assertNotNull($timerReconnect);
619+
$timerReconnect();
620+
}
621+
586622
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
587623
{
588624
if (method_exists($this, 'expectException')) {

0 commit comments

Comments
 (0)