feat: add client id to connection

This commit is contained in:
Giovani Rodriguez
2021-07-06 01:59:14 -04:00
parent 03d4a6afef
commit 8687ea3a26

View File

@@ -1,8 +1,8 @@
import asyncio import asyncio
from os import stat
import websockets import websockets
import json import json
import queue # refer to https://docs.python.org/3/library/queue.html for multi-threading import queue # refer to https://docs.python.org/3/library/queue.html for multi-threading
import uuid
from typing import Dict from typing import Dict
from threading import Thread from threading import Thread
@@ -11,6 +11,7 @@ class MultiplayerService():
_receive_piece_queue = queue.Queue() _receive_piece_queue = queue.Queue()
_send_piece_queue = queue.Queue() _send_piece_queue = queue.Queue()
_current_game_id = None _current_game_id = None
_client_id = str(uuid.uuid4())
@classmethod @classmethod
def init(cls) -> None: def init(cls) -> None:
@@ -70,7 +71,9 @@ class _NetworkConnectionService():
if not cls._join_game: if not cls._join_game:
return return
json_message = json.dumps({"action": "enter_game", "gameId": MultiplayerService._current_game_id}) json_message = json.dumps({"action": "enter_game",\
"clientId": MultiplayerService._client_id,\
"gameId": MultiplayerService._current_game_id})
await cls._websocket.send(json_message) await cls._websocket.send(json_message)
json_response = await cls._websocket.recv() json_response = await cls._websocket.recv()
@@ -88,12 +91,13 @@ class _NetworkConnectionService():
piece = MultiplayerService._send_piece_queue.get() piece = MultiplayerService._send_piece_queue.get()
# construct json message # construct json message
message = {} json_message = json.dumps({"action": "send_piece",\
message["action"] = "send_piece" "clientId": MultiplayerService._client_id,\
message["gameId"] = MultiplayerService._current_game_id "gameId": MultiplayerService._current_game_id,\
message["piece"] = piece.__dict__() "piece": piece.__dict__})
await cls._websocket.send(json.dumps(message)) await cls._websocket.send(json_message)
MultiplayerService._send_piece_queue.task_done()
@classmethod @classmethod
async def _try_receive_message(cls) -> None: async def _try_receive_message(cls) -> None: