有没有办法在Mojolicious :: Lite中访问websocket请求中的session
,像这样?
get '/' => sub {
my $self = shift;
$self->session->{foo} = 'bar';
$self->render('index');
};
websocket '/ws' => sub {
my $self = shift;
$self->on(message =>
sub {
my ($self, $msg) = @_;
my $foo = $self->session->{foo}; # <<<<< is actually empty
}
);
};
任何提示?
答案 0 :(得分:0)
我最终将签名的cookie值存储在隐藏的div中,然后将其与每个WS消息一起发送,作为“协议”的一部分。在消息处理程序中,我检查cookie值的存在,检查它的签名然后base64-decode并像往常一样使用它,如下所示:
sub decode_cookie {
my ($self, $cookie_val) = @_;
my $json = Mojo::JSON->new();
my $secret = $self->stash->{'mojo.secret'};
if ($cookie_val =~ s/--([^\-]+)$//) {
my $sig = $1;
#app->log->debug("cookie=".$cookie_val);
#app->log->debug("sig=".$sig);
my $check = Mojo::Util::hmac_sha1_sum $cookie_val, $secret;
unless(Mojo::Util::secure_compare $sig, $check) {
app->log->warn("invalid session cookie signature");
return;
}
} else {
app->log->warn("failed to extract cookie value");
return;
}
my $session = $json->decode(Mojo::Util::b64_decode($cookie_val));
unless($session) {
app->log->warn("failed to b64-decode session cookie");
return;
}
return $session;
}
websocket '/rtc' => sub {
my $self = shift;
my $json = Mojo::JSON->new;
$self->on(message =>
sub {
my ($self, $data) = @_;
app->log->debug("received WS message: ".$data);
$data = $json->decode($data);
my $session = decode_cookie($self, $data->{cval});
# do whatever you have to here...
}
);