Ejemplo n.º 1
0
 /**
  * Returns generator that yields new event when it's available on stream.
  *
  * @return Event[]
  */
 public function getEvents()
 {
     $buffer = '';
     $body = $this->response->getBody();
     while (true) {
         // if server close connection - try to reconnect
         if ($body->eof()) {
             // wait retry period before reconnection
             sleep($this->retry / 1000);
             $this->connect();
             // clear buffer since there is no sense in partial message
             $buffer = '';
         }
         $buffer .= $body->read(1);
         if (preg_match(self::END_OF_MESSAGE, $buffer)) {
             $parts = preg_split(self::END_OF_MESSAGE, $buffer, 2);
             $rawMessage = $parts[0];
             $remaining = $parts[1];
             $buffer = $remaining;
             $event = Event::parse($rawMessage);
             // if message contains id set it to last received message id
             if ($event->getId()) {
                 $this->lastId = $event->getId();
             }
             // take into account server request for reconnection delay
             if ($event->getRetry()) {
                 $this->retry = $event->getRetry();
             }
             (yield $event);
         }
     }
 }
Ejemplo n.º 2
0
 public function testCompleteExample()
 {
     $event = Event::parse("event: event type\nid: 20\nretry: 200\ndata: hello\ndata: world!");
     $this->assertEquals('event type', $event->getEventType());
     $this->assertEquals('20', $event->getId());
     $this->assertEquals(200, $event->getRetry());
     $this->assertEquals("hello\nworld!", $event->getData());
 }