2012-01-17 151 views
2

我想建立一个事件机器的ontop系统,它会检测TCP连接何时失败,并测试是否可以触发重新连接。我已经浏览了所有的eventmachine代码,但似乎无法找到连接回调的位置,无论是超时还是重新连接。尽管我已经在代码中设置了时间,但挂起的连接没有回调,如果我尝试重新启用连接,则不会反馈连接是成功还是失败。我正在使用它来有效地连接到一个telnet接口。检测eventmachine断开连接并测试重新连接

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

任何帮助将不胜感激。

回答

1

EventMachine的为此提供的解除绑定方法:

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 

注意解除绑定方法会叫上断开是否触发断开与否。

2
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