diff --git a/comprl/client/networking.py b/comprl/client/networking.py index 236c40b..d9a14ee 100644 --- a/comprl/client/networking.py +++ b/comprl/client/networking.py @@ -155,9 +155,15 @@ def connect_agent(agent: IAgent, host: str = "localhost", port: int = 65335): None """ + + def __on_error(reason): + agent.on_error(f"Connection to the server failed: {reason}.") + if reactor.running: + reactor.stop() + connectProtocol( - TCP4ClientEndpoint(reactor, host, port), + TCP4ClientEndpoint(reactor, host, port, timeout=30), ClientProtocol(agent), # we lose the protocol here, is this a problem? - ) + ).addErrback(__on_error) reactor.run() # type: ignore[attr-defined] diff --git a/examples/hockey/weak_hockey_agent.py b/examples/hockey/weak_hockey_agent.py index 1a64726..3bf0898 100644 --- a/examples/hockey/weak_hockey_agent.py +++ b/examples/hockey/weak_hockey_agent.py @@ -26,3 +26,6 @@ def on_end_game(result, stats): Weak_Hockey_Agent.run("token" + str(input("enter 1, 2, 3 or 4 to choose token: "))) + +# restarting the reactor is not easy. This might work: +# https://www.blog.pythonlibrary.org/2016/09/14/restarting-a-twisted-reactor/