前几天msn很糟,总是连不上,导致线程死掉了,这才想起要加上一个timeout,例:
加了两个Timeout,测试在线和重新连接。设60秒也差不多了。
机器人所有源码在:[url]http://code.google.com/p/whereisitcn/[/url]
def self.set_online_xmpp_with_retry_login(j)
begin
Timeout::timeout(60) {
p=Jabber::Presence.new(:chat)
j.send(p)
Robot.debug_log("set online for xmpp")
}
rescue => d
Robot.exception_log("set online error:",d)
begin
Timeout::timeout(60) {
connect_xmpp()
Robot.debug_log("reconnected for xmpp")
}
rescue => detail
Robot.exception_log("reconnect failed, please restart. error:",detail)
end
end
end
加了两个Timeout,测试在线和重新连接。设60秒也差不多了。
机器人所有源码在:[url]http://code.google.com/p/whereisitcn/[/url]
本文介绍了一种处理MSN XMPP连接不稳定的方法,通过设置60秒的超时来实现线程的安全退出与自动重连。该方案有效地解决了因连接失败导致的程序崩溃问题。
171万+

被折叠的 条评论
为什么被折叠?



