Detecting eventmachine disconnections and testing for reconnect

1.2k views Asked by At

I'm trying to build a system ontop of event machine that will detect when a TCP connection has failed and will test to see if a reconnect can be fired. I've gone through all of the eventmachine code but can't seem to find where there is a callback for the connection either timing out in action or in reconnection. Even though I've set the times in the code, there is no callback on the pending connect, and if I try to re fire reconnect I get no feedback as to whether the connection has succeeded or failed. I'm using it to connect to effectively a telnet interface.

EventMachine.run do
c = EventMachine.connect "10.8.1.99",5000,ConnectInterface
c.pending_connect_timeout = 10

end

Any help would be greatly appreciated.

2

There are 2 answers

0
Schmurfy On BEST ANSWER

EventMachine provide the unbind method for this:

module ConnectInterface
  def connection_completed
    puts "connected"
  end

  def unbind
    puts "disconnected"
  end
end


EM::run do
  EM::connect("10.8.1.99", 5000, ConnectInterface)
end

be aware The unbind method will get called on disconnect whether you triggered the disconnect or not.

0
Fanatic23 On
module MyCallBack

def unbind  # define your unbind method
  puts "#{@@ip}: #{@@port}"
  puts "-- disconnected from remote server!"
  puts "-- attempting reconnection"
  reconnect @@ip, @@port # use reconnect, already provided by EventMachine 
end

end