From 543e67929bbad7e5b827bd3e3801aec2e9678077 Mon Sep 17 00:00:00 2001 From: FReenen Date: Mon, 12 Aug 2024 23:35:07 +0200 Subject: [PATCH] gs: fix conn check --- ground-station.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ground-station.py b/ground-station.py index 5968fb5..0388bfb 100644 --- a/ground-station.py +++ b/ground-station.py @@ -17,13 +17,15 @@ Boats = [] Clients = [] async def sendToClient(client, msg): - if client['state'] != 'terminated' and client['ws'].connected: - await client['ws'].send(msg) - elif client['state'] != 'kicked': - client['state'] = 'terminated' - if client['boat'] is not None: - client['boat']['state'] = BOAT_STATE_AVAILABLE - client['boat'] = None + if client['state'] != 'terminated': + try: + await client['ws'].send(msg) + except ConnectionClosed: + if client['state'] != 'kicked': + client['state'] = 'terminated' + if client['boat'] is not None: + client['boat']['state'] = BOAT_STATE_AVAILABLE + client['boat'] = None async def sendToBoat(boat, cmd): boat['log'].append({"t": datetime.timestamp(datetime.now()), "type": "tx", "msg": cmd}) @@ -195,10 +197,11 @@ async def new_boat(boatId, name, ws): """handler for every new boat connection""" boat = { "id": boatId, "name": name, "ws": ws, "state": BOAT_STATE_AVAILABLE, "lastMsg": 0, "log": [] } print("new boat connected: " + boatId) - for bo in Boats: + for i, bo in enumerate(Boats): if bo['id'] == boat['id']: bo['state'] = BOAT_STATE_TERMINATED - await bo['ws'].close() + # await bo['ws'].close() + # del Boats[i] Boats.append(boat) return boat