Skip to content

Commit

Permalink
feat(server): advertise Lobby on mDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
JM-Lemmi committed Mar 18, 2024
1 parent 9d220b5 commit a25335c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Server/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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)
Expand All @@ -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({
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
websockets==12.0
jsonschema==4.21.1
emoji==2.10.1
zeroconf

0 comments on commit a25335c

Please sign in to comment.