Browse files

Try for reconnecting mechanism

  • Loading branch information...
1 parent 88367a4 commit 1dd43ca0deb76db02b04fbf9b4fab0bc0ec3ea2b @xHire committed Mar 10, 2013
Showing with 36 additions and 4 deletions.
  1. +8 −0 lib/irc.rb
  2. +28 −4 rubircot.rb
View
8 lib/irc.rb
@@ -37,6 +37,7 @@ def invitation
until code == '376'
msg = nil
msg = self.recv.match(/^:(\S+) (\d{3}) (\w*) :(.*)$/) while msg == nil
+ @server = msg[1]
code = msg[2]
end
end
@@ -152,4 +153,11 @@ def user str
def join channel
self.put "JOIN ##{channel}"
end
+
+ def ping
+ # PING rajaniemi.freenode.net
+ self.put "PING #{@server}"
+ msg = self.recv
+ #res = msg.match /^:([^\s]+) (PONG) ([^\s]*) :(.*)$/
+ end
end
View
32 rubircot.rb
@@ -46,7 +46,31 @@ def exit
end
### Run the bot
-# connect and join
-$bot.connect
-# listen for all messages and react to them
-loop { $bot.get }
+begin
+ # connect and join
+ $bot.connect
+ # listen for all messages and react to them
+ loop do
+ begin
+ timeout 300 do
+ $bot.get
+ end
+ rescue Timeout::Error
+ timeout 5 do
+ $bot.ping
+ end
+ end
+ end
+rescue Timeout::Error
+ puts "Connection timeouted. Reconnecting..."
+ $bot.quit
+ retry
+rescue Errno::ECONNRESET
+ puts "Connection reset by peer. Reconnecting..."
+ $bot.quit
+ retry
+rescue EOFError, Errno::EPIPE
+ puts "Error when reading from socket. Reconnecting..."
+ $bot.quit
+ retry
+end

0 comments on commit 1dd43ca

Please sign in to comment.