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

@@ -3,5 +3,5 @@ function _message_call(data)
{ {
local compile_string = "try { " + data["data"] + " } catch(id) { print(\"[PyG2O] Error white executing the code: \" + id + \"\\nCode: " + data["data"] + "\"); return null; }"; local compile_string = "try { " + data["data"] + " } catch(id) { print(\"[PyG2O] Error white executing the code: \" + id + \"\\nCode: " + data["data"] + "\"); return null; }";
local result = compilestring(compile_string)(); local result = compilestring(compile_string)();
_send({"uuid": data["uuid"], "data": result}); _send({"event": "sq_response", "uuid": data["uuid"], "data": result});
} }

View File

@@ -1,4 +1,6 @@
import inspect import inspect
import server
from fastapi import WebSocket
_EVENTS = {} _EVENTS = {}
@@ -15,9 +17,12 @@ def event(event_name: str, priority: int = 9999):
return func return func
return inlineEvent return inlineEvent
async def call_event(event_name: str, *args, **kwargs): async def call_event(event_name: str, connection: WebSocket, uuid: str | None, *args, **kwargs):
if event_name not in _EVENTS: if event_name not in _EVENTS:
return return
for item in _EVENTS[event_name]: for item in _EVENTS[event_name]:
await item['function'](*args, **kwargs) result = await item['function'](*args, **kwargs)
if uuid is None or result is None:
return
await server.Server.send(connection = connection, uuid = uuid, message = result)

View File

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