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

Copy link

@STQT666 STQT666 left a comment

Choose a reason for hiding this comment

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

fix the "websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost." issue

@xiaoshenxian
Copy link

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

@binance binance deleted a comment Oct 31, 2024
@binance binance deleted a comment Oct 31, 2024
@chsivateja
Copy link

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

@xiaoshenxian
Copy link

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.

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.

6 participants