def _connect(self, port):
"""Connect to the websocket, retrying as needed. Returns the socket."""
was_running = False
for i in range(120):
is_running = self.running
was_running = was_running or is_running
if (i >= 30 or was_running) and not is_running:
logging.warning(
"SC2 isn't running, so bailing early on the websocket connection.")
break
logging.info("Connection attempt %s (running: %s)", i, is_running)
time.sleep(1)
try:
return websocket.create_connection("ws://127.0.0.1:%s/sc2api" % port,
timeout=2 * 60) # 2 minutes
except socket.error:
pass # SC2 hasn't started listening yet.
except websocket.WebSocketException as err:
if "Handshake Status 404" in str(err):
pass # SC2 is listening, but hasn't set up the /sc2api endpoint yet.
else:
raise
sys.exit("Failed to create the socket.")
评论列表
文章目录