Пример #1
0
 public function start()
 {
     $packet = new ConnectRequestPacket();
     $packet->setProtocolLevel($this->connection->getProtocol());
     $packet->setKeepAlive($this->connection->getKeepAlive());
     $packet->setClientID($this->connection->getClientID());
     $packet->setCleanSession($this->connection->isCleanSession());
     $packet->setUsername($this->connection->getUsername());
     $packet->setPassword($this->connection->getPassword());
     $will = $this->connection->getWill();
     if ($will !== null && $will->getTopic() !== '' && $will->getPayload() !== '') {
         $packet->setWill($will->getTopic(), $will->getPayload(), $will->getQosLevel(), $will->isRetained());
     }
     return $packet;
 }
 /**
  * Registers a new client with the broker.
  *
  * @param Connection $connection
  * @param int        $timeout
  *
  * @return ExtendedPromiseInterface
  */
 private function registerClient(Connection $connection, $timeout)
 {
     $deferred = new Deferred();
     $responseTimer = $this->loop->addTimer($timeout, function () use($deferred, $timeout) {
         $exception = new \RuntimeException(sprintf('No response after %d seconds.', $timeout));
         $deferred->reject($exception);
     });
     $this->startFlow(new OutgoingConnectFlow($connection, $this->identifierGenerator), true)->always(function () use($responseTimer) {
         $this->loop->cancelTimer($responseTimer);
     })->then(function (Connection $connection) use($deferred) {
         $this->timer[] = $this->loop->addPeriodicTimer(floor($connection->getKeepAlive() * 0.75), function () {
             $this->startFlow(new OutgoingPingFlow());
         });
         $deferred->resolve($connection);
     })->otherwise(function (\Exception $e) use($deferred) {
         $deferred->reject($e);
     });
     return $deferred->promise();
 }