Gamsner
Gamsner

Reputation: 167

What is the better way to establish and reconnect to websocket in python?

With code below, I can open up connections to different websockets by threading. This connections stay stable until the sockets server starts new. This can be more than a week. Form then on, no new data coming in. So it seems the reconnect doesn't work well.

Now I did search in the web for another syntax and would like to know what would be best or better way in your view.

Here the actual code in action which runs for a while and then no more. I cannot see any error code in log.

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()

Here the code I have seen on web research with question what you think what would be better.

wst = threading.Thread(target=ws.run_forever(ping_interval=70, ping_timeout=10))
wst.daemon = True
wst.start()

any idea why my code stops catching data btw?

Upvotes: 2

Views: 2290

Answers (1)

Gamsner
Gamsner

Reputation: 167

The solution was:

The proxy from server side did not close the connection but did no more send any data, so that why it seemed the connection was closed, but was still there.

The solution in code was that we development a ping/pong, where we said, if there isnt any response from ping for longer than a while, then reconnect.

Upvotes: 2

Related Questions