Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a websocket callback: on_disconnected() #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

daboooooo
Copy link

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.

Copy link

@garbusbeach garbusbeach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey man, this is awesome. I've monkeypatched it similar in my project, but this is solution which I'm looking for

@garbusbeach
Copy link

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

@MFRealG
Copy link

MFRealG commented Mar 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants