gs: fix conn check

This commit is contained in:
Laila van Reenen 2024-08-12 23:35:07 +02:00
parent b2dfdcc14a
commit 543e67929b
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769

View File

@ -17,13 +17,15 @@ Boats = []
Clients = [] Clients = []
async def sendToClient(client, msg): async def sendToClient(client, msg):
if client['state'] != 'terminated' and client['ws'].connected: if client['state'] != 'terminated':
await client['ws'].send(msg) try:
elif client['state'] != 'kicked': await client['ws'].send(msg)
client['state'] = 'terminated' except ConnectionClosed:
if client['boat'] is not None: if client['state'] != 'kicked':
client['boat']['state'] = BOAT_STATE_AVAILABLE client['state'] = 'terminated'
client['boat'] = None if client['boat'] is not None:
client['boat']['state'] = BOAT_STATE_AVAILABLE
client['boat'] = None
async def sendToBoat(boat, cmd): async def sendToBoat(boat, cmd):
boat['log'].append({"t": datetime.timestamp(datetime.now()), "type": "tx", "msg": 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""" """handler for every new boat connection"""
boat = { "id": boatId, "name": name, "ws": ws, "state": BOAT_STATE_AVAILABLE, "lastMsg": 0, "log": [] } boat = { "id": boatId, "name": name, "ws": ws, "state": BOAT_STATE_AVAILABLE, "lastMsg": 0, "log": [] }
print("new boat connected: " + boatId) print("new boat connected: " + boatId)
for bo in Boats: for i, bo in enumerate(Boats):
if bo['id'] == boat['id']: if bo['id'] == boat['id']:
bo['state'] = BOAT_STATE_TERMINATED bo['state'] = BOAT_STATE_TERMINATED
await bo['ws'].close() # await bo['ws'].close()
# del Boats[i]
Boats.append(boat) Boats.append(boat)
return boat return boat