コード例 #1
0
 /**
  * @param string $data
  * @param array  $context
  */
 protected function doPush($data, array $context)
 {
     $config = $this->getConfig();
     if (false === $this->connected) {
         if (!extension_loaded('amqp')) {
             throw new \RuntimeException(sprintf('%s pusher require %s php extension', get_class($this), 'amqp'));
         }
         $this->connection = new \AMQPConnection($config);
         $this->connection->connect();
         list(, $exchange) = Utils::setupConnection($this->connection, $config);
         $this->setConnected();
     }
     $resolver = new OptionsResolver();
     $resolver->setDefaults(['routing_key' => null, 'publish_flags' => AMQP_NOPARAM, 'attributes' => array()]);
     $context = $resolver->resolve($context);
     $exchange->publish($data, $context['routing_key'], $context['publish_flags'], $context['attributes']);
 }
コード例 #2
0
 /**
  * @param LoopInterface       $loop
  * @param WampServerInterface $app
  */
 public function handle(LoopInterface $loop, WampServerInterface $app)
 {
     $config = $this->pusher->getConfig();
     $connection = new \AMQPConnection($config);
     $connection->connect();
     list(, , $queue) = Utils::setupConnection($connection, $config);
     $this->consumer = new Consumer($queue, $loop, 0.1, 10);
     $this->consumer->on('consume', function (\AMQPEnvelope $envelop, \AMQPQueue $queue) use($app, $config) {
         try {
             /** @var MessageInterface $message */
             $message = $this->serializer->deserialize($envelop->getBody());
             $request = $this->router->match(new Topic($message->getTopic()));
             $app->onPush($request, $message->getData(), $this->getName());
             $queue->ack($envelop->getDeliveryTag());
             $this->eventDispatcher->dispatch(Events::PUSHER_SUCCESS, new PushHandlerEvent($message, $this));
         } catch (\Exception $e) {
             $this->logger->error('AMQP handler failed to ack message', ['exception_message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $envelop->getBody()]);
             $queue->reject($envelop->getDeliveryTag());
             $this->eventDispatcher->dispatch(Events::PUSHER_FAIL, new PushHandlerEvent($envelop->getBody(), $this));
         }
         $this->logger->info(sprintf('AMQP transport listening on %s:%s', $config['host'], $config['port']));
     });
 }