From 6881f41f8e10bf6f7fd806454f8b8359b9f3d325 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 18 Aug 2025 11:37:51 +0200 Subject: swiftstory: move out of legacy websockets 14.0 implementation https://websockets.readthedocs.io/en/stable/howto/upgrade.html Signed-off-by: Olivier Gayot --- swiftstory/__main__.py | 11 +++++------ swiftstory/client.py | 4 ++-- swiftstory/interface/ws.py | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) (limited to 'swiftstory') diff --git a/swiftstory/__main__.py b/swiftstory/__main__.py index 3e2bcff..453f25f 100644 --- a/swiftstory/__main__.py +++ b/swiftstory/__main__.py @@ -6,13 +6,13 @@ import argparse import asyncio import logging -from websockets.server import serve +from websockets.asyncio.server import serve from swiftstory.game_manager import GameManager from swiftstory.interface.ws import WebsocketsInterface -def main() -> None: +async def main() -> None: """ Entry point: we create the game manager and start the Websockets server. """ parser = argparse.ArgumentParser() @@ -23,12 +23,11 @@ def main() -> None: logging.basicConfig(level=logging.INFO) ws_interface = WebsocketsInterface(GameManager()) - start_server = serve(ws_interface.connection_handler, + server = await serve(ws_interface.connection_handler, args['listen'], args['port']) - asyncio.get_event_loop().run_until_complete(start_server) - asyncio.get_event_loop().run_forever() + await server.serve_forever() if __name__ == '__main__': - main() + asyncio.run(main()) diff --git a/swiftstory/client.py b/swiftstory/client.py index cd5e82a..0831cb0 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -5,7 +5,7 @@ import logging from typing import Optional import websockets.exceptions -from websockets.server import WebSocketServerProtocol +from websockets.asyncio.server import ServerConnection from swiftstory.game import Game from swiftstory.game_manager import GameManager @@ -17,7 +17,7 @@ class Client: """ Represent a client. A client manages a (web)socket to communicate with the outside world. It also manages the associated player when in a game. """ - def __init__(self, socket: WebSocketServerProtocol, game_manager: GameManager) -> None: + def __init__(self, socket: ServerConnection, game_manager: GameManager) -> None: self.game: Optional[Game] = None self.game_manager: GameManager = game_manager diff --git a/swiftstory/interface/ws.py b/swiftstory/interface/ws.py index 082ed1a..e9150dd 100644 --- a/swiftstory/interface/ws.py +++ b/swiftstory/interface/ws.py @@ -7,7 +7,7 @@ import logging from typing import Union import websockets.exceptions -from websockets.server import WebSocketServerProtocol +from websockets.asyncio.server import ServerConnection from swiftstory.client import Client from swiftstory.exception import WrongAction, JoinError @@ -20,7 +20,7 @@ class WebsocketsInterface: def __init__(self, game_manager: GameManager): self.game_manager = game_manager - async def connection_handler(self, websocket: WebSocketServerProtocol, path: str) -> None: + async def connection_handler(self, websocket: ServerConnection) -> None: """ Coroutine that handles one websocket connection. """ client = Client(websocket, self.game_manager) -- cgit v1.2.3