From 3d6ae21a7d85d46c2ec8db2f902cf68572e595dd Mon Sep 17 00:00:00 2001 From: nolan Date: Tue, 30 Sep 2025 22:49:43 -0400 Subject: [PATCH] Updated server to cancel the response task if a new request is submitted --- server.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index 8e2c822..77fd28b 100644 --- a/server.py +++ b/server.py @@ -18,14 +18,19 @@ class WebSocketServer: async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something print(f"{connection.remote_address} Connected") connected: bool = True + task: asyncio.Task = None while (connected): raw: str = await connection.recv() - message = json.loads(raw) - print(f"Received: {message["message"]} width id {message["ID"]}") - time.sleep(random.randrange(1, 10) / 10) - response = {"ID": message["ID"], "message": f"received: {message["message"]}"} - await connection.send(json.dumps(response)) - print("Server replied") + if task != None and task.cancel(): print("TASK CANCELED") + task = asyncio.create_task(self.respond(raw, connection)) + + async def respond(self, raw: str, connection: ServerConnection) -> None: + message = json.loads(raw) + print(f"Received: {message["message"]} width id {message["ID"]}") + await asyncio.sleep(random.randrange(1, 10) / 10) + response = {"ID": message["ID"], "message": f"received: {message["message"]}"} + await connection.send(json.dumps(response)) + print("Server replied") def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this print("Server controller. Now die")