Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python websocket client connected status - how to update so I get correct status?

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?

like image 635
Trewq Avatar asked Feb 01 '15 03:02

Trewq


People also ask

How do I fix my WebSocket connection?

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.

How do I check my WebSocket connection status?

In the search field, enter websocket . From the search results, click WebSocket Connection Status.

How does Python connect to WebSocket client?

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.


1 Answers

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.

like image 134
falsetru Avatar answered Oct 26 '22 19:10

falsetru