From 674a81c5b230ec4c7c426192d18caefffab56204 Mon Sep 17 00:00:00 2001 From: Julian Lemmerich <41118534+JM-Lemmi@users.noreply.github.com> Date: Thu, 22 Feb 2024 11:11:42 +0100 Subject: [PATCH] running websocket server and testclient framework given for future message handling Co-authored-by: Bengt Wegner --- Server/player.py | 8 +++++--- Server/statistics.py | 3 +++ Server/websocket_server.py | 37 ++++++++++++++++++++++++++++++++++ Server/websocket_testclient.py | 19 +++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 Server/statistics.py create mode 100644 Server/websocket_server.py create mode 100644 Server/websocket_testclient.py diff --git a/Server/player.py b/Server/player.py index 52e53ff..0652338 100644 --- a/Server/player.py +++ b/Server/player.py @@ -1,3 +1,5 @@ +from statistics import Statistics + class Player: """ Represents a player in the game. @@ -22,7 +24,7 @@ class RemotePlayer(Player): statistics (Statistics): The statistics of the player. """ def __init__(self, id: int, display_name: str, statistics: Statistics): - super(id, display_name, statistics) + super().__init__(id, display_name, statistics) class LocalPlayer(Player): """ @@ -34,7 +36,7 @@ class LocalPlayer(Player): statistics (Statistics): The statistics of the player. """ def __init__(self, id: int, display_name: str, statistics: Statistics): - super(id, display_name, statistics) + super().__init__(id, display_name, statistics) class ComputerPlayer(Player): """ @@ -46,4 +48,4 @@ class ComputerPlayer(Player): statistics (Statistics): The statistics of the player. """ def __init__(self, id: int, display_name: str, statistics: Statistics): - super(id, display_name, statistics) + super().__init__(id, display_name, statistics) diff --git a/Server/statistics.py b/Server/statistics.py new file mode 100644 index 0000000..a63117c --- /dev/null +++ b/Server/statistics.py @@ -0,0 +1,3 @@ +class Statistics: + def __init__(self) -> None: + pass \ No newline at end of file diff --git a/Server/websocket_server.py b/Server/websocket_server.py new file mode 100644 index 0000000..c835c6e --- /dev/null +++ b/Server/websocket_server.py @@ -0,0 +1,37 @@ +from game import Game +import asyncio +import websockets +import logging +import json + +logger = logging.getLogger() + +async def handler(websocket): + while True: + try: + message = await websocket.recv() + except websockets.ConnectionClosedOK: + logger.info("Connection Closed from Client-Side") + # TODO: Add handling when game is not over yet + break + + # TODO: Catch other errors for disconnects + + logger.info(f"Received: {message}") + + message_json = json.loads(message) + match message_json["message_type"]: + case "join": + pass + case _: + await websocket.send("Invalid Message Type") + + +async def main(): + async with websockets.serve(handler, "", 8001): + await asyncio.Future() # run forever + + +if __name__ == "__main__": + asyncio.run(main()) + diff --git a/Server/websocket_testclient.py b/Server/websocket_testclient.py new file mode 100644 index 0000000..131c440 --- /dev/null +++ b/Server/websocket_testclient.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +import asyncio +from websockets.sync.client import connect +import json + +def hello(): + with connect("ws://localhost:8001") as websocket: + + obj = { + "message_type": "asdf", + "username": "my_username" + } + + websocket.send(json.dumps(obj)) + message = websocket.recv() + print(f"Received: {message}") + +hello()