Prerequisites: run the Mojolicious client script with debugging information:
MOJO_EVENTEMITTER_DEBUG=1 MOJO_USERAGENT_DEBUG=1 perl mua.pl
As in version 7.43, it Mojo::UserAgenthas a built-in connection pool , but specifically refuses to use it for WebSockets . This may be due to the fact that, as you said, WebSockets is stateful, and if UserAgent blindly reuses connections, it can cause chaos. However, if your application knows how to safely reuse them, this is another matter.
IRC Mojolicious, sri, , :
16:28 sri mohawk: some frameworks like phoenix have their own higher level protocol on top of websockets to multiplex multiple channels https://hexdocs.pm/phoenix/channels.html
16:28 sounds like that what you want
16:28 mojolicious should have something like that, but doesn't yet
[...]
16:42 sri it not hard to build on top of mojolicious, but for now you have to do that yourself
16:42 ultimately i'd hope for us to have it in core, without the message bus part
16:43 but channel management and routing
[...]
16:50 jberger mohawk I did write Mojolicious::Plugin::Multiplex which might help
16:51 For an example of a higher level tool
, OP :
, , .
, , , . , , :
use strict;
use warnings;
use Mojo::UserAgent;
use Time::HiRes qw(time);
$| = 1;
my $REQ = {
type => "ping",
reqid => 0,
};
my $URL = "wss://trello.com/1/Session/socket";
my $SECONDS = 2;
my $POOL_SIZE = 5;
my $ua = Mojo::UserAgent->new;
my @pool;
sub make_conn {
my ($ua, $url, $pool) = @_;
$ua->websocket($URL => sub {
my (undef, $tx) = @_;
die "error: ", $tx->res->error->{message}, "\n" if $tx->res->error;
die 'Not a websocket connection' unless $tx->is_websocket;
push @$pool, $tx;
});
}
sub send_message {
my ($pool, $request, $start) = @_;
my $tx = shift @$pool;
die "got bad connection" unless $tx;
$tx->once(message => sub {
my (undef, $msg) = @_;
print "got back: $msg\n";
print "took: ", time - $start, "\n";
push @$pool, $tx;
});
$tx->send({json => $request});
}
make_conn($ua, $URL, \@pool) for (1..5);
my $timer_cb;
$timer_cb = sub {
my $loop = shift;
print "every $SECONDS\n";
send_message(\@pool, $REQ, time);
$loop->timer($SECONDS => $timer_cb);
};
Mojo::IOLoop->timer($SECONDS => $timer_cb);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
:
, , , 5 .
time . ( ) 300 , , . , 120 .