Hello Perl Monks, I'm looking after your wisdom on IO::Socket::INET (more exactly, IO::Socket:SSL). I am using it for IRC (yea, I know there's POE and AnyEvent for it, but I like the freedom it gives to you). And I want to code a timer that checks if the connection is timeout (ie didn't get server PING after 60 seconds or more).

The current structure is the following:

#!/usr/bin/perl use strict; use warnings; use utf8; use IO::Socket::SSL; my $server = irc.REDACTED.us my $nick = 'wiseau'; my $login = 'theroom'; my $channel = 'redacted'; my $sock = new IO::Socket::SSL( PeerAddr => $server, PeerPort => 6697, Proto => 'tcp') or die "cannot connect\n"; print $sock "NICK ".$nick."\r\n"; print $sock "USER ".$login." * * :tommy wiseau\r\n"; while (my $input = <$sock>) { if ($input =~ /004/) { last; } print $input; if ($input =~ /^PING/) { my @pong1 = split(':', $input); my $pong = $pong1[1]; print $sock "PONG ".$pong."\r\n"; } } print $sock "JOIN ".$channel."\r\n"; while (chomp(my $input = <$sock>)) { print "$input\n"; if ($input =~ /^PING(.*)$/i) { print $sock "PONG ".$1."\r\n"; } if ($input =~ /hello/) { print $sock "PRIVMSG ".$channel." :Hello World"."\r\n"; } }

The code is actually more complicated but that snippet is the representation of the barebones, with while statements for each line, etc. It prints the raw IRC socket into STDOUT, and when you "ping out" (the act of no longer being able to reply a PING from the server) it just stays that way, and IO::Socket::SSL won't know that you actually disconnected, thus keeping the programme open all the time. Is there a way to make a "last PING request" timer to make the programme self-aware about its connection status? (Tried thinking about putting it in the last while loop but it wouldn't work because when you loose your connection it'll just cease to print, but the socket will stay open).


In reply to IRC PING Timer by AndresSnape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.