python websocket客户端连接状态 - 如何更新,以便我得到正确的状态?

时间:2015-02-01 03:16:08

标签: python node.js websocket

我可以使用websocket连接到node.js python websocket服务器。当我问python websocket客户端它是否连接到服务器时,它是不准确的。

以下是发生的事情:

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://localhost:8081") # my websocket server is running, so all good here.
>>> ws.connected
True      # This is correct. Now I kill the websocker server
>>> ws.connected
True      # this should be false since the server is dead.

ws node.js模块附带wscat,您可以通过命令wscat -l 8081运行websocket服务器。

如何获得指示真正连接/断开状态的准确状态?

1 个答案:

答案 0 :(得分:3)

.connectedclose,...已调用或shutdown接收空数据外,recv*属性未更新。

尝试拨打recv(),然后检查.connected属性。