From 7d39216a3bbea50223ac4a6d7a9e6343b8247886 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 23 Dec 2021 19:43:47 +0100 Subject: Don't store a reference to a client from the player The only reason why we had a reference was that we were able to send notifications back to the client. Instead, we now store the notifications at the player level. At the client level, we now have a coroutine that waits for notifications from the player and sends them when available. Signed-off-by: Olivier Gayot --- swiftstory/client.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'swiftstory/client.py') diff --git a/swiftstory/client.py b/swiftstory/client.py index 527c9c6..c3a2aff 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -66,12 +66,16 @@ class Client: raise WrongAction('You have to join a game first') return self.game.try_view_black_card(self.player) - def register_notification(self, message): + def monitor_player(self) -> None: + """ Start monitoring the player for notifications and send them. """ async def f(): - try: - await self.socket.send(message) - except websockets.exceptions.ConnectionClosed: - logging.warning("Recipient has disconnected.") + assert self.player is not None + while True: + try: + notif = await self.player.notifications.get() + await self.socket.send(notif) + except websockets.exceptions.ConnectionClosed: + logging.warning("Recipient has disconnected.") asyncio.create_task(f()) -- cgit v1.2.3