Websocket “Connection to remote host was lost” after responding a 'Pong' to a 'Ping' from Binance Server

I use websocket-client to build the long-live connection and receive Future data from Binance Server: wss://fstream.binance.com:443/ws
As the document of websocket-client says it will automatically respond the ‘Pong’ once a ‘Ping’ received, but I still receive ‘Connection to remote host was lost’ almost ever 8-13 mins, so I add a manual Pong in the on_ping() method and ensure a ‘Pong’ is sent, but I still got below error after few seconds.

Here is my code of on_ping():

    def on_ping(ws, message):
        ws.send('Send a pong', opcode=websocket.ABNF.OPCODE_PONG)
        log.logger.info('收到服务器Ping')

Below is the error message:
2023-11-07 15:06:16,836 - : 收到服务器Ping
2023-11-07 15:10:51,620 - : --------------- Websocket 出现下列异常,现在重新连接 ---------------
2023-11-07 15:10:51,622 - : Connection to remote host was lost.
2023-11-07 15:10:51,627 - : Traceback (most recent call last):
File “websocket_app.py”, line 443, in read
File “websocket_core.py”, line 406, in recv_data_frame
File “websocket_core.py”, line 445, in recv_frame
File “websocket_abnf.py”, line 352, in recv_frame
File “websocket_abnf.py”, line 373, in recv_strict
File “websocket_core.py”, line 529, in _recv
File “websocket_socket.py”, line 122, in recv
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

Any advice?

Hey,
Thanks for sharing your issue.
There could be several reasons for your disconnection:

  • The websocket-client library might have an issue. Check with other users to see if they encounter the same problem;
  • There are various rules to follow to maintain a WebSocket connection. Make sure you are following all of them. You can find them all on this link: Binance API Documentation
  • It might be a network issue. Check with your internet provider.