Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top