pyCraft icon indicating copy to clipboard operation
pyCraft copied to clipboard

[Question] Suprised I haven't seen this asked, I simply want this to auto-reconnect

Open Credit-Score opened this issue 4 years ago • 3 comments

Just like how 5zig can keep attempting to auto-reconnect you every 30 seconds, how could I add that functionality to this?

I added a print to def disconnect() inside of connection.py, but it never even fired when turning off my own wifi to test the exact use case of my internet going out.

Edit: In fact, it doesn't seem to ever recognize I've disconnected at all until I send a chat message.
And I'm very tired so I forgot to mention that I have in fact read #146 but there is a decent amount of back and forth, and you saying you've implemented some change, but I'm not exactly sure what I need where to actually have a working auto-reconnect.

Credit-Score avatar Jan 09 '21 23:01 Credit-Score

I could write something myself that would just simply call back to main() to try reconnecting if I had some way of knowing if I was disconnected.

The def disconnect(self, immediate=False) didn't fire until I tried to send a chat message, as I said before, and that's the only place I see self.disconnected set to False :/.

Credit-Score avatar Jan 10 '21 20:01 Credit-Score

I used this simple trick and it seemed to work. In the start I simply stored the connection object in an array so I can reference it later. And added an exit handler that reconnects the connection request.

def main(self={}):
    options = get_options()
    def handle_exit():
        print("Connection Lost, Reconnecting...")
        self["connection"].connect()

    if options.offline:
        print("Connecting in offline mode...")
        connection = Connection(
            options.address, options.port, username=options.username, handle_exit=handle_exit)
    else:
        auth_token = authentication.AuthenticationToken()
        try:
            auth_token.authenticate(options.username, options.password)
        except YggdrasilError as e:
            print(e)
            sys.exit()
        print("Logged in as %s..." % auth_token.profile.name)
        connection = Connection(
            options.address, options.port, auth_token=auth_token, handle_exit=handle_exit)
        self["connection"] = connection

NotTahaAli avatar May 06 '21 13:05 NotTahaAli

the disconnect function is used to actively disconnect to the server. you should use listener on disconnect packet or use handle_exit

Cassy-Lee avatar Apr 22 '23 07:04 Cassy-Lee