vendor/gos/web-socket-bundle/src/Pusher/Amqp/AmqpServerPushHandler.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Gos\Bundle\WebSocketBundle\Pusher\Amqp;
  3. use Gos\Bundle\WebSocketBundle\Event\PushHandlerFailEvent;
  4. use Gos\Bundle\WebSocketBundle\Event\PushHandlerSuccessEvent;
  5. use Gos\Bundle\WebSocketBundle\GosWebSocketEvents;
  6. use Gos\Bundle\WebSocketBundle\Pusher\AbstractServerPushHandler;
  7. use Gos\Bundle\WebSocketBundle\Pusher\Message;
  8. use Gos\Bundle\WebSocketBundle\Router\WampRouter;
  9. use Gos\Bundle\WebSocketBundle\Server\App\PushableWampServerInterface;
  10. use Gos\Component\ReactAMQP\Consumer;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerAwareTrait;
  13. use Ratchet\Wamp\Topic;
  14. use React\EventLoop\LoopInterface;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. trigger_deprecation('gos/web-socket-bundle''3.1''The "%s" class is deprecated and will be removed in 4.0, use the symfony/messenger component instead.'AmqpServerPushHandler::class);
  18. /**
  19.  * @deprecated to be removed in 4.0, use the symfony/messenger component instead
  20.  */
  21. final class AmqpServerPushHandler extends AbstractServerPushHandler implements LoggerAwareInterface
  22. {
  23.     use LoggerAwareTrait;
  24.     private WampRouter $router;
  25.     private SerializerInterface $serializer;
  26.     private Consumer $consumer;
  27.     private EventDispatcherInterface $eventDispatcher;
  28.     private AmqpConnectionFactoryInterface $connectionFactory;
  29.     private \AMQPConnection $connection;
  30.     public function __construct(
  31.         WampRouter $router,
  32.         SerializerInterface $serializer,
  33.         EventDispatcherInterface $eventDispatcher,
  34.         AmqpConnectionFactoryInterface $connectionFactory
  35.     ) {
  36.         $this->router $router;
  37.         $this->serializer $serializer;
  38.         $this->eventDispatcher $eventDispatcher;
  39.         $this->connectionFactory $connectionFactory;
  40.     }
  41.     public function handle(LoopInterface $loopPushableWampServerInterface $app): void
  42.     {
  43.         $this->connection $this->connectionFactory->createConnection();
  44.         $this->connection->connect();
  45.         $this->consumer = new Consumer($this->connectionFactory->createQueue($this->connection), $loop0.110);
  46.         $this->consumer->on(
  47.             'consume',
  48.             function (\AMQPEnvelope $envelope\AMQPQueue $queue) use ($app): void {
  49.                 try {
  50.                     /** @var Message $message */
  51.                     $message $this->serializer->deserialize($envelope->getBody(), Message::class, 'json');
  52.                     $request $this->router->match(new Topic($message->getTopic()));
  53.                     $app->onPush($request$message->getData(), $this->getName());
  54.                     $queue->ack($envelope->getDeliveryTag());
  55.                     $this->eventDispatcher->dispatch(new PushHandlerSuccessEvent($envelope->getBody(), $this), GosWebSocketEvents::PUSHER_SUCCESS);
  56.                 } catch (\Exception $e) {
  57.                     if (null !== $this->logger) {
  58.                         $this->logger->error(
  59.                             'AMQP handler failed to ack message',
  60.                             [
  61.                                 'exception' => $e,
  62.                                 'message' => $envelope->getBody(),
  63.                             ]
  64.                         );
  65.                     }
  66.                     $queue->reject($envelope->getDeliveryTag());
  67.                     $this->eventDispatcher->dispatch(new PushHandlerFailEvent($envelope->getBody(), $this), GosWebSocketEvents::PUSHER_FAIL);
  68.                 }
  69.                 if (null !== $this->logger) {
  70.                     $this->logger->info(
  71.                         sprintf(
  72.                             'AMQP transport listening on %s:%s',
  73.                             $this->connection->getHost(),
  74.                             $this->connection->getPort()
  75.                         )
  76.                     );
  77.                 }
  78.             }
  79.         );
  80.     }
  81.     public function close(): void
  82.     {
  83.         if (null !== $this->consumer) {
  84.             $this->consumer->emit('close_amqp_consumer');
  85.         }
  86.         if (null !== $this->connection) {
  87.             $this->connection->disconnect();
  88.         }
  89.     }
  90. }