【问题标题】:Detecting eventmachine disconnections and testing for reconnect检测事件机器断开连接并测试重新连接
【发布时间】:2012-02-11 22:31:21
【问题描述】:
我正在尝试在事件机器上构建一个系统,该系统将检测 TCP 连接何时失败,并测试是否可以触发重新连接。我已经浏览了所有的 eventmachine 代码,但似乎无法找到连接回调的位置,无论是在操作中超时还是在重新连接中。即使我在代码中设置了时间,挂起的连接也没有回调,如果我尝试重新启动重新连接,我不会收到关于连接是成功还是失败的反馈。我正在使用它来有效地连接到 telnet 接口。
EventMachine.run do
c = EventMachine.connect "10.8.1.99",5000,ConnectInterface
c.pending_connect_timeout = 10
结束
任何帮助将不胜感激。
【问题讨论】:
标签:
ruby
sockets
telnet
eventmachine
reconnect
【解决方案1】:
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
【解决方案2】:
EventMachine 为此提供了 unbind 方法:
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
请注意,无论您是否触发了断开连接,都会在断开连接时调用 unbind 方法。