feat: add client id to connection
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import asyncio
|
||||
from os import stat
|
||||
import websockets
|
||||
import json
|
||||
import queue # refer to https://docs.python.org/3/library/queue.html for multi-threading
|
||||
import uuid
|
||||
from typing import Dict
|
||||
from threading import Thread
|
||||
|
||||
@@ -11,6 +11,7 @@ class MultiplayerService():
|
||||
_receive_piece_queue = queue.Queue()
|
||||
_send_piece_queue = queue.Queue()
|
||||
_current_game_id = None
|
||||
_client_id = str(uuid.uuid4())
|
||||
|
||||
@classmethod
|
||||
def init(cls) -> None:
|
||||
@@ -70,7 +71,9 @@ class _NetworkConnectionService():
|
||||
if not cls._join_game:
|
||||
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)
|
||||
|
||||
json_response = await cls._websocket.recv()
|
||||
@@ -88,12 +91,13 @@ class _NetworkConnectionService():
|
||||
piece = MultiplayerService._send_piece_queue.get()
|
||||
|
||||
# construct json message
|
||||
message = {}
|
||||
message["action"] = "send_piece"
|
||||
message["gameId"] = MultiplayerService._current_game_id
|
||||
message["piece"] = piece.__dict__()
|
||||
json_message = json.dumps({"action": "send_piece",\
|
||||
"clientId": MultiplayerService._client_id,\
|
||||
"gameId": MultiplayerService._current_game_id,\
|
||||
"piece": piece.__dict__})
|
||||
|
||||
await cls._websocket.send(json.dumps(message))
|
||||
await cls._websocket.send(json_message)
|
||||
MultiplayerService._send_piece_queue.task_done()
|
||||
|
||||
@classmethod
|
||||
async def _try_receive_message(cls) -> None:
|
||||
|
||||
Reference in New Issue
Block a user