Mocholic reuse of a previously established compound

I am trying to reuse a previously established connection to a web server in order to avoid handshaking with websocket. I found that a websocket user transaction can be built using build_websocket_tx(more here ), and there is a connection identifier for each network connection, which can be obtained using the routine connectiondefined in Mojo::Transaction(more details here ). Can I somehow combine both of these options to reuse the connection? Is there any other way to do this?

PS: Websocket connections must be serial and reusable. But Mojolicoious does not provide such parameters for network connections.

EDIT

Sample code without reusing the connection.

#!/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 {
                    my ($tx, $msg) = @_;
                    print "$msg\n";
                    $tx->closed; #Close connection
                });
            $tx->send(encode_json($req));
        });
}

sub reuse_conn {
    # Re use connection
}

_connect();

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
+4
source share
1 answer

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 :

, , .

, , , . , , :

#!/usr/bin/perl

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

# pool gets pushed onto, shifted off, so using recently-used connection
sub send_message {
  my ($pool, $request, $start) = @_;
  my $tx = shift @$pool;
  die "got bad connection" unless $tx; # error checking needs improving
  $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); # establish pool

# every 2 secs, send a message
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
  • send_message
  • , " "

, , , 5 .

time . ( ) 300 , , . , 120 .

+2

Source: https://habr.com/ru/post/1682982/


All Articles