/**
  * @param string $data
  * @param array  $context
  */
 protected function doPush($data, array $context)
 {
     if (false === $this->isConnected()) {
         $config = $this->getConfig();
         $this->connection = new Client($config['host'], $config['port'], $config['ssl'], $config['origin']);
         $this->connection->connect('/');
         $this->setConnected();
     }
     $message = $this->serializer->deserialize($data);
     $this->connection->publish($message->getTopic(), json_encode($message->getData()));
 }
 /**
  * @param LoopInterface       $loop
  * @param WampServerInterface $app
  */
 public function handle(LoopInterface $loop, WampServerInterface $app)
 {
     $config = $this->getConfig();
     $context = new Context($loop);
     /* @var SocketWrapper $pull */
     $this->consumer = $context->getSocket(\ZMQ::SOCKET_PULL);
     $this->logger->info(sprintf('ZMQ transport listening on %s:%s', $config['host'], $config['port']));
     $this->consumer->bind('tcp://' . $config['host'] . ':' . $config['port']);
     $this->consumer->on('message', function ($data) use($app, $config) {
         try {
             /** @var MessageInterface $message */
             $message = $this->serializer->deserialize($data);
             $request = $this->router->match(new Topic($message->getTopic()));
             $app->onPush($request, $message->getData(), $this->getName());
             $this->eventDispatcher->dispatch(Events::PUSHER_SUCCESS, new PushHandlerEvent($data, $this));
         } catch (\Exception $e) {
             $this->logger->error('AMQP handler failed to ack message', ['exception_message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $data]);
             $this->eventDispatcher->dispatch(Events::PUSHER_FAIL, new PushHandlerEvent($data, $this));
         }
     });
 }
 /**
  * @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']));
     });
 }
 /**
  * @param array|string $data
  * @param string       $routeName
  * @param array[]      $routeParameters
  * @param array        $context
  *
  * @return string|\Symfony\Component\Serializer\Encoder\scalar
  */
 public function push($data, $routeName, array $routeParameters = array(), array $context = [])
 {
     $channel = $this->router->generate($routeName, $routeParameters);
     $message = new Message($channel, $data);
     return $this->doPush($this->serializer->serialize($message), $context);
 }