websocket-client icon indicating copy to clipboard operation
websocket-client copied to clipboard

How to make a persistent connection

Open ArunEG opened this issue 8 years ago • 3 comments

If the websocket object lost the connection to the server how we can re-connect it without affecting the application. write now it is throwing

WebSocketConnectionClosedException

ArunEG avatar Feb 22 '18 12:02 ArunEG

I have a problem that may be the same as you, I recieve data maybe for 1 minute, and server give me a None, then go to do _socket.py if not bytes: raise WebSocketConnectionClosedException("Connection is already closed.") When go to do raise WebSocketConnectionClosedException("Connection is already closed."), I want to send something to server for keeping alive. Could I do that? right now, I send message every 30s to avoid WebSocketConnectionClosedException,is there a better way?

skyjerry149 avatar Apr 29 '18 13:04 skyjerry149

I have a problem that may be the same as you, I recieve data maybe for 1 minute, and server give me a None, then go to do _socket.py if not bytes: raise WebSocketConnectionClosedException("Connection is already closed.") When go to do raise WebSocketConnectionClosedException("Connection is already closed."), I want to send something to server for keeping alive. Could I do that? right now, I send message every 30s to avoid WebSocketConnectionClosedException,is there a better way?

It's amazing that I use 30s replace with 45s and then it never raise exceptions again.

ouyangjx avatar May 13 '19 09:05 ouyangjx

If you are getting an error stating that the connection is closed, then either 1. the client closed the connection or 2. the server closed the connection.

  1. To keep the client listening in a long-lived connection, it is better to use the WebSocketApp class rather than the WebSocket class. Examples of both options are given in the examples documentation and in the README.md.
  2. If the server is closing the connection, you may be able to keep the connection alive by setting a ping interval, which will ping the server at a certain interval (for example, 30 seconds or 45 seconds) in an attempt to keep the connection alive.

For future reference, this is similar to #281.

engn33r avatar Feb 16 '21 02:02 engn33r