I am able to connect to a node.js websocket server using python websocket. When I ask the python websocket client if it is connected to the server, it is not accurate.
Here is what happens:
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.
The ws node.js module comes with wscat
and you can run a websocket server by the command wscat -l 8081
.
How do I get an accurate status that indicates the true connected/disconnected state?
Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.
In the search field, enter websocket . From the search results, click WebSocket Connection Status.
WebSocket Client with PythonCreate a new File “client.py” and import the packages as we did in our server code. Now let's create a Python asynchronous function (also called coroutine). async def test(): We will use the connect function from the WebSockets module to build a WebSocket client connection.
The .connected
attribute is not updated, unless close
or shutdown
, ... called, or recv*
receive empty data.
Try to call recv()
, then check the .connected
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With