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.