in reply to mojo::websocket reconnect

Hello! Try this:
use strict; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $url = "ws://some.url/path"; my $globals; my $connect_ws; $connect_ws = sub { my ($ua, $tx) = @_; $tx->on(json => sub { my ($tx, $json) = @_; # populate $globals here and listen for $opcode = 7 # if ($opcode eq 7) { $tx->finish(); } }); $tx->on(finish => sub { my ($ws, $code, $reason) = @_; print "WebSocket closed with status $code. $reason\n"; $ua->websocket($url => $connect_ws); }); # if we have some globals, then we're resuming - send tokens etc if ($globals) { $tx->send({ json => $globals }) } }; $ua->websocket($url => $connect_ws); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Maybe this answer will tell you more: https://stackoverflow.com/a/70136684

Replies are listed 'Best First'.
Re^2: mojo::websocket reconnect
by james28909 (Deacon) on May 23, 2022 at 07:15 UTC

    I just wanted to tell you that this actually helped me with another problem i was going to face. I was going to need to open a completely new websocket to a different endpoint, but doing it like you described above sure did make that a very simple task. Thanks again!!!

      You're welcome!
Re^2: mojo::websocket reconnect
by james28909 (Deacon) on May 20, 2022 at 18:11 UTC
    Excellent! This worked great. Thanks!