binance-futures-connector-python icon indicating copy to clipboard operation
binance-futures-connector-python copied to clipboard

add a websocket callback: on_disconnected()

Open daboooooo opened this issue 2 years ago • 3 comments

I want to reconnect to websocket server, but can't catch the disconnected exception raised by the BinanceSocketManager thread. So add the on_disconnected() callback to solve this problem.

daboooooo avatar Dec 24 '23 15:12 daboooooo

Does anyone maintain this repo? Quite critical issue is resolved here.....

garbusbeach avatar Feb 08 '24 15:02 garbusbeach

I want to reconnect to websocket server, but can't catch the disconnected exception raised by the BinanceSocketManager thread. So add the on_disconnected() callback to solve this problem.

Hi @daboooooo , Thanks a lot for your idea. Why just not to use: self._callback(self.on_error, e) as it already exist instead of creating new event: on_disconnected? In this case we need to patch: binance_socket_manager.py only. And also to add self.ws.connected = False when any exception is raised in binance_socket_manager.py? Later on, we will be able to intercept/catch the Websocket exception and can check status of self.ws.connected like this guy propose: #120

Something like this?

            except WebSocketException as e:
                if isinstance(e, WebSocketConnectionClosedException):
                    self.logger.error("Lost websocket connection")
                else:
                    self.logger.error("Websocket exception: {}".format(e))
                if self.ws.connected:
                    self.ws.send_close()
                self.ws.connected = False
                self._callback(self.on_error, e)
                break
            except Exception as e:
                self.logger.error("Exception in read_data: {}".format(e))
                if self.ws.connected:
                   self.ws.send_close()
                self.ws.connected = False
                self._callback(self.on_error, e)
                break

Bl4ck-K1ng avatar Mar 30 '24 00:03 Bl4ck-K1ng

Just for your information, the new version of websocket-client introduced a on_reconnect callback which might be helpful for this issue.

xiaoshenxian avatar Sep 22 '24 01:09 xiaoshenxian

Just for your information, the new version of websocket-client introduced a on_reconnect callback which might be helpful for this issue.

Can you point to this in code. Not able to find it in latest

chsivateja avatar Nov 19 '24 10:11 chsivateja

Just for your information, the new version of websocket-client introduced a on_reconnect callback which might be helpful for this issue.

Can you point to this in code. Not able to find it in latest

https://github.com/websocket-client/websocket-client/blob/52329c4956f88133f45ecd510668776cd8a96ebf/websocket/_app.py#L59

The latest version is 1.8.0 which already supports this.

xiaoshenxian avatar Nov 21 '24 01:11 xiaoshenxian

We have released a new modularised version of the Python Connector and the binance-futures-connector-python will not be maintained anymore. I suggest you to start using the new one and open a new issue at the modularised connector, if you still encounter it.

alplabin avatar Jul 18 '25 13:07 alplabin