Python&Autobahn with Twisted:在重新连接clientfactory中重置maxRetries

2024-09-29 19:24:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python&Autobahn with Twisted重新连接客户机,如果由于某种原因连接“断开”。在

有一个很好的例子here使用重新连接clientfactory设置,但是我的问题是关于maxRetries。在

在脚本执行期间,总共允许设置5。在

class EchoClientFactory(ReconnectingClientFactory, WebSocketClientFactory):
    protocol = EchoClientProtocol
    # http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ReconnectingClientFactory.html
    #
    maxDelay = 10
    maxRetries = 5
    def startedConnecting(self, connector):
        print('Started to connect.')
    def clientConnectionLost(self, connector, reason):
        print('Lost connection. Reason: {}'.format(reason))
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
    def clientConnectionFailed(self, connector, reason):
        print('Connection failed. Reason: {}'.format(reason))
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

例如,如果出现问题,客户端在重试2次后连接,则只剩下3次,并且在成功连接后,号码不会“重置”。在

如何实现这一点,也就是说,如果可能,在成功连接后将maxRetries重置回5?在

谢谢!在

科斯塔斯


Tags: selfformatconnectordefwithprotocol重置reason
1条回答
网友
1楼 · 发布于 2024-09-29 19:24:59

安排协议在成功连接后调用ReconnectingClientFactory.resetDelay。这将重置所有内容,以便重新开始回退逻辑。在

应用程序需要调用此方法,而不是自动重置状态,因为即使TCP连接成功,也可能存在连接问题,需要使用回退重试。例如,服务器可能会用“太忙,稍后再试”消息响应您的身份验证尝试。如果这类事情可以在你的协议中发生,你应该在这些事情发生的机会过去之后,把你的resetDelay调用ReconnectingClientFactory不知道这是什么时候。在

相关问题 更多 >

    热门问题