Mojolicious re-using a previously established connection

Viewed 496

I'm trying to reuse a previously established websocket connection to avoid the websocket handshake. I found that a custom websocket transaction can be built using build_websocket_tx (more details here), and there's a connection identifier for every websocket connection which can be retrieved using connection subroutine defined in Mojo::Transaction (more details here). Can I somehow combine both of these to re use the connection? Is there another way to do so?

PS: Websocket connections are supposed to be consistent and reusable. But Mojolicoious doesn't provide any such options for websocket connections.

EDIT

Example code without connection re-use.

#!/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;
1 Answers
Related