gpt4 book ai didi

Perl Redis 监听不订阅 channel

转载 作者:可可西里 更新时间:2023-11-01 11:14:49 26 4
gpt4 key购买 nike

此代码似乎无法正常工作,我不知道如何调试它。我正在使用 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 位作为助手时,这终于起作用了

所以在路由文件中;

$self->helper(redis => sub {
state $redis = Mojo::Redis->new('redis://anything:jejakredis1234!@localhost:6379/');
});

在 Controller 中

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)
});
}

关于Perl Redis 监听不订阅 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53505078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com