首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConnectionLost异常在扭体中的处理

ConnectionLost异常在扭体中的处理
EN

Stack Overflow用户
提问于 2011-01-31 20:27:52
回答 1查看 7.4K关注 0票数 4

我无法处理ConnectionLost异常。我所拥有的简单例子。首先,iIset将连接到jabber服务器并对其进行ping。我用的是wokkel图书馆。然后将errback添加到发送ping的方法中。在回错中,我处理ConnectionLost错误。在那之后,我关闭了互联网连接。但我看不出这个ConnectionLost是否被处理了。我关闭应用程序中的连接,并调用所有异常处理程序。

平做得很好。

代码语言:javascript
复制
[XmlStream,client] Ping to JID(u'jabber.ru') started at HivemindPingClientProtocol 
[-] SEND: «iq to='jabber.ru' type='get' id='H_3'>/>»
[XmlStream,client] RECV: "/><feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"

互联网连接已关闭

代码语言:javascript
复制
[-] SEND: «iq to='jabber.ru' type='get' id='H_6'>/>»
[-] SEND: «iq to='jabber.ru' type='get' id='H_7'>/>»

不调用ConnectionLost的处理程序。“在HivemindXMPPClient上关闭的流”在StreamManager中用_disconnected方法打印

代码语言:javascript
复制
[-] Protocol stopped
[-] Protocol closed
[-] Transport stopped
[XmlStream,client] Stream closed at HivemindXMPPClient

关闭流后将处理所有异常。

代码语言:javascript
复制
[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion.
[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion.] 
[XmlStream,client] Connection lost with [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.] 
[XmlStream,client] Stopping factory <hivemind.network.network_core.HivemindXmlStreamFactory object at 0xa2e904c>> 

有人能解释为什么在关闭流后会调用回错吗?实际上,我想实现一个重新连接特性(我已经使用了ReconnectingFactory,但它对ConnectionLost没有反应)。有人能给我一些例子,重新连接实现的扭曲吗?

脚本示例。运行此脚本(确保ping工作正常)。然后关闭你的互联网连接。当发生多个ping时,您需要终止脚本。如您所见,ConnectionLost错误是在关闭连接后处理的。

代码语言:javascript
复制
import sys
from twisted.python import log
from twisted.words.protocols import jabber
from twisted.internet.error import ConnectionLost
from wokkel.client import XMPPClient
from wokkel.ping import PingClientProtocol
from twisted.internet.task import LoopingCall

JID = unicode('YOUR@JABBER.ID')
PASSWORD = 'PASSWORD'
INTERVAL = 3

class SpecialPingClientProtocol(PingClientProtocol):

    def __init__(self, entity, interval):
        self.__entity = jabber.jid.internJID(entity)
        self.__interval = interval
        self.__pingLoop = None

    def _onError(self, failure):
        log.msg('Failure %s at %s' % (failure, self.__class__.__name__))
        error = failure.trap(jabber.error.StanzaError, ConnectionLost)
        if error == jabber.error.StanzaError:
            if failure.value.condition == 'feature-not-implemented':
                return None
        elif error == ConnectionLost:
            # Do some beautiful things
            log.msg('Connection is lost. I want to reconnect NOW')
        return failure

    def _sendPing(self):
        defer = self.ping(self.__entity)
        defer.addErrback(self._onError)

    def stopPing(self):
        log.msg('Ping to %s stopped at %s' % (self.__entity, self.__class__.__name__))
        if self.__pingLoop is not None and self.__pingLoop.running:
            self.__pingLoop.stop()
            self.__pingLoop = None

    def startPing(self):
        log.msg('Ping to %s started at %s ' % (self.__entity, self.__class__.__name__))
        self.__pingLoop = LoopingCall(self._sendPing)
        self.__pingLoop.start(self.__interval, now = False)

def main():
    log.startLogging(sys.stdout)
    transport = XMPPClient(jabber.jid.internJID(JID), PASSWORD)
    transport.logTraffic = True
    pinger = SpecialPingClientProtocol(JID, INTERVAL)
    pinger.setHandlerParent(transport)
    transport.startService()
    pinger.startPing()
    reactor.run()

if __name__ == '__main__':
    from twisted.internet import reactor
    main()
EN

回答 1

Stack Overflow用户

发布于 2011-05-22 02:20:38

议定书包括:

代码语言:javascript
复制
clientConnectionFailed(self, connector, reason)
clientConnectionLost(self, connector, reason)

您可以覆盖两者,并调用PingClientProtocol.clientConnectionFailed和PingClientProtocol.clientConnectionLost。

假设PingClientProtocol以某种方式继承协议

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4855720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档