diff --git a/Server/websocket_server.py b/Server/websocket_server.py index b4b3cec..9f1f25c 100644 --- a/Server/websocket_server.py +++ b/Server/websocket_server.py @@ -8,7 +8,8 @@ import logging import json from jsonschema import validate, ValidationError -from uuid import UUID +import uuid +from zeroconf import ServiceInfo, Zeroconf logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @@ -26,6 +27,13 @@ def __init__(self, admin:Player, port: int = 8765) -> None: with open("./json_schema/client_to_server.json", "r") as f: self._json_schema = json.load(f) + # MDNS + #https://stackoverflow.com/a/74633230 + self._mdns = Zeroconf() + wsInfo = ServiceInfo(type_ = '_tictactoe._tcp.local.', name = 'tttk-'+str(uuid.uuid4())+'._tictactoe._tcp.local.', port = self._port) + self._mdns.register_service(wsInfo) + + async def handler(self, websocket): self._connections.add(websocket) @@ -50,7 +58,7 @@ async def handler(self, websocket): await websocket.send("Game in progress, cannot join") # TODO jsonify break - self._players[message_json["profile"]["uuid"]] = Player(uuid=UUID(message_json["profile"]["uuid"]), display_name=message_json["profile"]["display_name"], color=message_json["profile"]["color"]) + self._players[message_json["profile"]["uuid"]] = Player(uuid=uuid.UUID(message_json["profile"]["uuid"]), display_name=message_json["profile"]["display_name"], color=message_json["profile"]["color"]) # send new lobby status websockets.broadcast(self._connections, json.dumps({ @@ -91,6 +99,7 @@ async def handler(self, websocket): self._game = Game(player1 = list(self._players.values())[0], player2 = list(self._players.values())[1], rule_base = rulebase) self._inprogress = True + self._mdns.unregister_all_services() websockets.broadcast(self._connections, json.dumps({ "message_type": "game/start", @@ -204,5 +213,5 @@ def run(self): asyncio.run(self.start_server()) if __name__ == "__main__": - lobby = Lobby(port = 8765, admin = Player(uuid=UUID("c4f0eccd-a6a4-4662-999c-17669bc23d5e"), display_name="admin", color=0xffffff, ready=True)) + lobby = Lobby(port = 8765, admin = Player(uuid=uuid.UUID("c4f0eccd-a6a4-4662-999c-17669bc23d5e"), display_name="admin", color=0xffffff, ready=True)) lobby.run() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4f5214a..e2f1a80 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ websockets==12.0 jsonschema==4.21.1 emoji==2.10.1 +zeroconf