File: chat-monitor.pl

package info (click to toggle)
libmojo-rabbitmq-client-perl 0.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: perl: 2,165; xml: 489; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use Mojo::RabbitMQ::Client;

$|++;

my $amqp
  = Mojo::RabbitMQ::Client->new(
  url => ($ENV{MOJO_RABBITMQ_URL} || 'rabbitmq://guest:guest@127.0.0.1:5672/')
  );
$amqp->on(
  open => sub {
    my ($self) = @_;

    my $channel = Mojo::RabbitMQ::Channel->new();
    $channel->on(
      open => sub {
        my $queue = $channel->declare_queue(exclusive => 1);
        $queue->on(
          success => sub {
            my $method = $_[1]->method_frame;
            my $bind   = $channel->bind_queue(
              exchange    => 'mojo',
              queue       => $method->queue,
              routing_key => '',
            );
            $bind->on(
              success => sub {
                my $consumer = $channel->consume(queue => $method->queue);
                $consumer->on(
                  message => sub {
                    print "<<< " . $_[1]->{body}->payload . " <<<\n";
                  }
                );
                $consumer->deliver();
              }
            );
            $bind->deliver();
          }
        );
        $queue->deliver();
      }
    );
    $self->open_channel($channel);
  }
);
$amqp->connect();

$amqp->start();