feat: Упрощено взаимодействие по вебсокетам между клиентом и сервером

This commit is contained in:
AURUMVORXX
2025-10-29 22:57:04 +05:00
parent aa6b5700ce
commit a31b565967
3 changed files with 16 additions and 13 deletions

View File

@@ -33,7 +33,7 @@ class Server:
cls._register_routes(app)
@classmethod
async def publish(cls, topic: str, message: str) -> asyncio.Future:
async def publish(cls, topic: str, message) -> asyncio.Future:
if topic not in cls._topics:
raise KeyError('Клиентов прослушивающих этот топик не существует')
@@ -49,7 +49,7 @@ class Server:
return request
@classmethod
async def send(cls, connection: WebSocket, message: str, uuid: str):
async def send(cls, connection: WebSocket, message, uuid: str):
data = {
'uuid': uuid,
'data': message,
@@ -133,14 +133,12 @@ class Server:
async def _process_message(cls, connection: WebSocket, message: dict):
match message:
case {'uuid': id, 'data': data}:
if id in cls._requests:
cls._requests[id].set_result(data)
else:
asyncio.create_task(call_event('onWebsocketMessage', connection, id, data))
case {'event': event, **args}:
asyncio.create_task(call_event(event, **args))
case {'event': event, **kwargs}:
try:
cls._requests[kwargs['uuid']].set_result(kwargs)
except KeyError:
uuid = kwargs.get('uuid')
asyncio.create_task(call_event(event, connection, uuid, **kwargs))
case {'subscribe': topics}:
await cls._subscribe(topics, connection)