通过下面的代码,我可以通过线程打开与不同websocket的连接。该连接将保持稳定,直到套接字服务器重新启动为止。这可能会超过一周。然后,没有新数据输入。因此,重新连接似乎无法正常工作。
现在,我确实在网络上搜索了另一种语法,并想知道您认为哪种方法最好或更好。
这里的实际代码运行了一段时间,然后不再运行。我在日志中看不到任何错误代码。
ws_connections = []
# websocket-client based connection due to issue in receiving data from some urls
func_message = partial(WSClientProtocol.on_message, factory)
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(
url,
on_message=func_message,
on_error=WSClientProtocol.on_error,
on_close=WSClientProtocol.on_close,
)
ws.on_open = WSClientProtocol.on_open
ws_connections.append(ws)
wst = threading.Thread(target=ws.run_forever, kwargs={'ping_interval': 5, 'ping_timeout' : 2})
wst.daemon = True
wst.start()
在这里,我在网络研究中看到的代码提出了疑问,您认为哪种方法更好。
wst = threading.Thread(target=ws.run_forever(ping_interval=70, ping_timeout=10))
wst.daemon = True
wst.start()
知道为什么我的代码停止捕获数据吗?
解决方案是:
服务器端的代理没有关闭连接,但是没有再发送任何数据,因此,为什么看起来连接已关闭,但仍然存在。
代码中的解决方案是我们开发一个ping / pong,我们说过,如果ping的响应时间超过一会儿,则重新连接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句