-
Notifications
You must be signed in to change notification settings - Fork 0
/
subs.pl
executable file
·45 lines (35 loc) · 986 Bytes
/
subs.pl
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
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::Redis2;
my $redis_server = 'localhost:6379';
$redis_server = $ENV{REDISCLOUD_URL} if $ENV{REDISCLOUD_URL};
helper redis => sub { shift->stash->{redis} ||= Mojo::Redis2->new(url => $redis_server)};
websocket '/sub/:channel' => sub {
my $c = shift;
my $schann = $c->stash('channel');
my $tx = $c->tx;
$c->redis->on(message => sub {
my ($self, $message, $channel) = @_;
$tx->send("M: $message, C: $channel");
});
$c->redis->subscribe("$schann" => sub {
my ($self, $err) = @_;
$c->app->log->error("Subscription error on channel $schann : $err") if $err;
});
$c->on(message => sub {
my ($ws, $bytes) = @_;
$c->send("echo: $bytes");
});
};
websocket '/pub' => sub {
my $c = shift;
my $tx = $c->tx;
$c->on(message => sub {
my ($ws, $bytes) = @_;
if ($bytes =~ /^(\S+)\s(.*)/) {
$c->redis->publish("$1" => "$2");
$c->send("Ok");
} else {$c->send("No")}
});
};
app->start;