【问题标题】:Autobahn: catch sendMessage error高速公路:捕获 sendMessage 错误
【发布时间】:2015-01-07 08:00:24
【问题描述】:

如何捕获 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

【问题讨论】:

    标签: python networking twisted autobahn


    【解决方案1】:

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

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

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

    • autoPingInterval
    • autoPingTimeout
    • autoPingSize

    serversclients 都可以设置。

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

    【讨论】:

    • WebSocketClientProtocol (0.9.4-2) 中的 _sendAutoPing 缺少 callLater。没有它,客户端不支持自动功能。我添加了它,现在一切正常。谢谢
    • 这样的话,能不能提个issue做个PR?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多