Ejemplo n.º 1
0
 /**
  * testRead method
  *
  * @return void
  */
 public function testRead()
 {
     $socket = new FileStreamSocket();
     $socket->connect(__DIR__ . '/../test_packets/connack_not_authorized.hex');
     $result = Packet::read($socket);
     $expected = pack('C*', 0x0, 0x5);
     $this->assertEquals($expected, $result->buffer());
 }
Ejemplo n.º 2
0
 /**
  * Listen on the socket and receive a single packet.
  *
  * @param $topic Only return packets from this topic (optional)
  * @return Packet
  */
 public function receivePacket($topic = null)
 {
     $socket = $this->socket();
     while (true) {
         if ($topic && !empty($this->inbox[$topic])) {
             return array_shift($this->inbox[$topic]);
         }
         if ($socket->dataAvailable()) {
             $packet = Packet::read($socket);
             if ($topic && $packet->topic() != $topic) {
                 $this->inbox[$packet->topic()] = $packet;
                 continue;
             }
             return $packet;
         }
     }
 }