我正在尝试重用以前建立的websocket连接,以避免websocket握手。我发现,一个自定义的WebSocket交易可以使用内置build_websocket_tx(详情点击这里),而且也为可使用检索每一个WebSocket连接的连接标识符connection中定义的子程序Mojo::Transaction(详情点击这里)。我可以通过某种方式将两者结合使用以重新使用连接吗?还有其他方法吗?
PS:Websocket连接应该是一致且可重用的。但是Mojolicoious没有为websocket连接提供任何此类选项。
无需连接即可重复使用的示例代码。
#!/usr/bin/perl
use strict;
use warnings;
use Mojo::UserAgent;
use JSON qw |encode_json|;
my $ua = Mojo::UserAgent->new;
my $url = "wss://trello.com/1/Session/socket";
$| = 1;
sub _connect {
my $req = {
type => "ping",
reqid=> 0
};
$ua->websocket(
$url => sub {
my ($ua, $tx) = @_;
die "error: ", $tx->res->error->{message}, "\n" if $tx->res->error;
die 'Not a websocket connection' unless $tx->is_websocket;
# Connection established.
$tx->on(
message => sub …Run Code Online (Sandbox Code Playgroud)