Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: How do I make a tcp socket heartbeat?

by pudda (Acolyte)
on Oct 09, 2020 at 19:05 UTC ( [id://11122642]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I make a tcp socket heartbeat?
in thread How do I make a tcp socket heartbeat?

Taking the opportunity, Could you tell me how I do if I wanted my client, when the server goes down, to try to reconnect?

Like, in the $stream->on(close => ...) how do try to I connect again to the server?

Replies are listed 'Best First'.
Re^3: How do I make a tcp socket heartbeat?
by haukex (Archbishop) on Oct 09, 2020 at 19:35 UTC

    One way to do that is to place the Mojo::IOLoop->client call in a subroutine, as in sub reconnect { Mojo::IOLoop->client( ... ) }, make sure to call that sub once before starting the loop to set up the initial connection, and then, in the handler for the close event, do something like Mojo::IOLoop->timer( 2 => \&reconnect ); to call that subroutine to reestablish the connection. I very much suggest the timer call so as to not flood the server with connection attempts, and I would also recommend adding a counter variable so you can stop attempting to reconnect after a certain number of failed attempts.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found