DynamicDevices / godot-multi

12 stars 1 forks source link

The mqtt library hangs if there is no connection found #2

Open goatchurchprime opened 3 years ago

goatchurchprime commented 3 years ago

The code here: https://github.com/DynamicDevices/godot-multi/blob/master/godot-multi/addons/mqtt/mqtt.gd#L90

has some bad busy infinite loops!

    while not self.client.is_connected_to_host():
        pass
    # todo: Add in a timeout
    while self.client.get_status() != StreamPeerTCP.STATUS_CONNECTED:
        pass
    print("Connected to server")

This should be something more like:

    var timeout = 20
    var timestep = 0.2
    while not self.client.is_connected_to_host():
        yield(get_tree().create_timer(timestep), "timeout")
        timeout -= timestep
        if timeout < 0:
            return false
    while self.client.get_status() != StreamPeerTCP.STATUS_CONNECTED:
        yield(get_tree().create_timer(timestep), "timeout")
        timeout -= timestep
        if timeout < 0:
            return false
ajlennon commented 3 years ago

Left as an exercise for the user ;)