高速公路:捕捉sendMessage错误

时间:2015-01-07 08:00:24

标签: python networking twisted autobahn

如何捕获sendMessage错误?没有异常传播。当服务器关闭(半开连接)时,我想在我的客户端代码中捕获错误。如果运行websocket守护程序的服务器崩溃,重新启动,丢失网络连接,则closeConnection / ConnectionLost / ConnectionFailed回调不起作用。

from autobahn.websocket import WebSocketClientProtocol

class ClientProtocol(WebSocketClientProtocol):
    def onOpen(self):
        def heartbeat():
            self.sendMessage("HB")
            self.factory.reactor.callLater(1, heartbeat)

        # I want to catch a socket error   
        try:
            heartbeat()
        except Exception as e:  
            print e

也许有更好的解决方案。乒乓 ?简单地说,我无法找到我的客户端如何检测服务器崩溃/重新启动然后重新连接的方式。

我正在使用高速公路0.6.5

1 个答案:

答案 0 :(得分:0)

是的,有一个更好的解决方案可以快速检测丢失的连接:WebSocket ping / pong。这内置于WebSocket协议中。

AutobahnPython支持自动WebSocket Ping / Pong ,主要针对这种情况(快速连接丢失检测和连接保持活动)。

您可以使用以下参数激活它:

  • autoPingInterval
  • autoPingTimeout
  • autoPingSize

可以为serversclients设置。

你需要一个最近的AutobahnPython版本(我认为是0.9.4+)。你的(0.6.5)没有这个功能。