Updated server to cancel the response task if a new request is submitted
This commit is contained in:
parent
a3042432f2
commit
3d6ae21a7d
17
server.py
17
server.py
@ -18,14 +18,19 @@ class WebSocketServer:
|
|||||||
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
|
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
|
||||||
print(f"{connection.remote_address} Connected")
|
print(f"{connection.remote_address} Connected")
|
||||||
connected: bool = True
|
connected: bool = True
|
||||||
|
task: asyncio.Task = None
|
||||||
while (connected):
|
while (connected):
|
||||||
raw: str = await connection.recv()
|
raw: str = await connection.recv()
|
||||||
message = json.loads(raw)
|
if task != None and task.cancel(): print("TASK CANCELED")
|
||||||
print(f"Received: {message["message"]} width id {message["ID"]}")
|
task = asyncio.create_task(self.respond(raw, connection))
|
||||||
time.sleep(random.randrange(1, 10) / 10)
|
|
||||||
response = {"ID": message["ID"], "message": f"received: {message["message"]}"}
|
async def respond(self, raw: str, connection: ServerConnection) -> None:
|
||||||
await connection.send(json.dumps(response))
|
message = json.loads(raw)
|
||||||
print("Server replied")
|
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
|
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
|
||||||
print("Server controller. Now die")
|
print("Server controller. Now die")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user