2

当使用 Twisted ReconnectingClientFactory 并且连接丢失时,我需要从 clientConnectionLost 方法中调用 connector.connect() 还是自动发生这种情况?

答案可能看起来很明显,因为它毕竟是 ReconnectingClientFactory但 Twisted 文档在这里说了一些让我想知道的东西:

“调用 connector.connect() 可能很有用 - 这将重新连接。”

术语“可能有用”的措辞和使用导致了这个问题,因为基本客户端工厂的 api 文档说了同样的话。

Max的答案是正确的,但经过进一步研究,我认为“校正者”的答案如下:

def clientConnectionLost(self, connector, reason):
    # do stuff here that is unique to your own requirements, then:
    ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

这允许您执行应用程序所需的特殊操作,然后调用工厂代码以允许 Twisted 为您调用 retry()。

4

2 回答 2

5

我的旧答案并不完全正确。而是这样做:

def clientConnectionLost(self, connector, reason):
    # do stuff here that is unique to your own requirements, then:
    ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

这允许您执行应用程序所需的特殊操作,然后调用工厂代码以允许 Twisted 为您调用 retry()。

于 2013-11-18T15:26:16.907 回答
1

调用 ReconnectingClientFactory.clientConnectionLost(self, connector, reason) 是正确的做法,因为它:

  1. 在调用 self.retry 之前检查“self.continueTrying”(这是关键,因为连接可能由于调用“stopTrying()”而丢失
  2. 将 self.connector 设置为传入的连接器。
  3. 调用 self.retry() (由于缺少传入的连接器,使用 #2 中设置的 self.connector)。
  4. 如果将来 ReconnectingClientFactory 实现发生更改,需要在重新连接路径中执行更多操作,它们将被自动处理而无需更改代码。
于 2015-04-22T22:55:35.003 回答