Perl Redis 监听不订阅频道
Perl Redis listen does not subscribe to channel
此代码似乎无法正常工作,我不知道如何调试它。我正在使用 Mojolicious 通过 websocket 发送订阅的消息。
use Mojo::Redis;
#Controller
sub data_stream {
my $c = shift;
$c->inactivity_timeout(300);
my $redis = Mojo::Redis-new('redis://xxxxxxxx@localhost:6379/');
my $pubsub = $redis->pubsub;
my $cb = $pubsub->listen('data' => sub {
my ($pubsub, $msg) = @_;
$c->app->log->debug("WS: $msg");
$c->send({text => $msg});
});
$c->on(finish => sub {
$c->app->log->debug("WS CONNECTION CLOSED!");
$pubsub->unlisten('data' => $cb)
});
}
上面的子从下面的路由器调用
$router->websocket('/data_stream')->to('grid#data_stream');
哈哈,这是我自己回答的第二个问题。
当我将 Mojo::Redis-new 位移动为 helper
时,这终于起作用了
因此在路由器文件中;
$self->helper(redis => sub {
state $redis = Mojo::Redis->new('redis://anything:jejakredis1234!@localhost:6379/');
});
并在控制器中
sub data_stream {
my $c = shift;
$c->inactivity_timeout(300);
my $pubsub = $c->redis->pubsub;
my $cb = $pubsub->listen('data' => sub {
my ($pubsub, $msg) = @_;
$c->app->log->debug("WS: $msg");
$c->send({text => $msg});
});
$c->on(finish => sub {
$c->app->log->debug("WS CONNECTION CLOSED!");
$pubsub->unlisten('data' => $cb)
});
}
此代码似乎无法正常工作,我不知道如何调试它。我正在使用 Mojolicious 通过 websocket 发送订阅的消息。
use Mojo::Redis;
#Controller
sub data_stream {
my $c = shift;
$c->inactivity_timeout(300);
my $redis = Mojo::Redis-new('redis://xxxxxxxx@localhost:6379/');
my $pubsub = $redis->pubsub;
my $cb = $pubsub->listen('data' => sub {
my ($pubsub, $msg) = @_;
$c->app->log->debug("WS: $msg");
$c->send({text => $msg});
});
$c->on(finish => sub {
$c->app->log->debug("WS CONNECTION CLOSED!");
$pubsub->unlisten('data' => $cb)
});
}
上面的子从下面的路由器调用
$router->websocket('/data_stream')->to('grid#data_stream');
哈哈,这是我自己回答的第二个问题。 当我将 Mojo::Redis-new 位移动为 helper
时,这终于起作用了因此在路由器文件中;
$self->helper(redis => sub {
state $redis = Mojo::Redis->new('redis://anything:jejakredis1234!@localhost:6379/');
});
并在控制器中
sub data_stream {
my $c = shift;
$c->inactivity_timeout(300);
my $pubsub = $c->redis->pubsub;
my $cb = $pubsub->listen('data' => sub {
my ($pubsub, $msg) = @_;
$c->app->log->debug("WS: $msg");
$c->send({text => $msg});
});
$c->on(finish => sub {
$c->app->log->debug("WS CONNECTION CLOSED!");
$pubsub->unlisten('data' => $cb)
});
}