Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

mojo::websocket reconnect

by james28909 (Deacon)
on May 19, 2022 at 17:49 UTC ( #11144015=perlquestion: print w/replies, xml ) Need Help??

james28909 has asked for the wisdom of the Perl Monks concerning the following question:

I am having some difficulty trying to understand how to reconnect this websocket. I have a simple discord bot that i am using to provide services to users at a website i moderate. Everything works as expected except when the gateway operation code '7' (server requests reconnect) is sent to my client. To reconnect/resume a connection, you must send session id, token and sequence number of the last event. I have all of this info already and am not to sure about how to reconnect/resume the websocket connection. I would ask at discord api chat but this isnt a discord problem, its a me not knowing how to reconnect with the lib i am using problem lol. EDIT: Before this code there are a POST and a GET request sent to their servers for authorization and logging the bot in. When i do this i get the session_id and token. those two values are stored in a %globals hash and accessed when needed to reconnect.

here is a snippet from my script:

my $ua = Mojo::UserAgent->new; $ua->inactivity_timeout(0); $ua->websocket( $gateway . ':443?v=9&application/json, text/plain, */*' => sub { my ( $ua, $tx ) = @_; # Check if WebSocket handshake was successful say 'WebSocket handshake failed!' and return unless $tx->is_we +bsocket; # Wait for WebSocket to be closed $tx->on( finish => sub { my ( $tx, $code, $reason ) = @_; say "WebSocket closed with status $code and $reason"; $ua->websocket->resume; #would i use the payload he +re somehow instead of using sub reconnect()? } ); $tx->on( message => sub { my ( $tx, $msg ) = @_; my $data = decode_json($msg); my %payload = %$data; $globals{'sequence'} = $payload{'s'} if defined $paylo +ad{'s'}; for ( $payload{'op'} ) { when ('7') { reconnect( $tx, %payload ) } } } ); } ); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; sub reconnect { my $reconnect_payload = { op => 6, d => { token => $globals{'token'}, session_id => $globals{'session_id'}, seq => defined( $globals{'sequence'} ) ? $globals{ +'sequence'} : 'null' }, s => 'null', t => 'null' }; #how to reconnect }

The code is not a drop in place and work kind of thing. There could be syntax errors but hopefully this gets the point across of what i am trying to figure out. I just cant figure out how to reconnect.

here are links to their api reference and i am pretty sure i have the payload setup right, I just dont know how to reconnect this websocket when requested.

resume structure sent by client to server to reconnect
server op code payload structure

Replies are listed 'Best First'.
Re: mojo::websocket reconnect
by alexander_lunev (Pilgrim) on May 19, 2022 at 20:43 UTC
    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:

      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!
      Excellent! This worked great. Thanks!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11144015]
Approved by LanX
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2023-03-27 16:13 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.