diff --git a/src/pyg2o/server.py b/src/pyg2o/server.py index d486b2e..a525703 100644 --- a/src/pyg2o/server.py +++ b/src/pyg2o/server.py @@ -49,6 +49,17 @@ class Server: return request + @classmethod + async def send(cls, connection: WebSocket, message: str, uuid: str): + data = { + 'uuid': uuid, + 'data': message, + } + data = json.dumps(data) + # Меняем синтаксис под Squirrel + data = data.replace("'", '\\"').replace('True', 'true').replace('False', 'false') + await connection.send_text(data) + @classmethod def _make_request(cls): request_id = str(uuid4()) @@ -65,8 +76,8 @@ class Server: @classmethod def _register_routes(cls, app): @app.websocket('/pyg2o') - async def pyg2o(websocket: WebSocket): - await cls._handle_connection(websocket) + async def pyg2o(websocket: WebSocket, token: str, topics: str): + await cls._handle_connection(websocket, token, topics) _ = pyg2o @@ -85,9 +96,9 @@ class Server: cls._topics[topic].discard(connection) @classmethod - async def _handle_connection(cls, connection: WebSocket): + async def _handle_connection(cls, connection: WebSocket, token: str, topics: str): - if not await cls._process_headers(connection): + if not await cls._process_query_params(connection, token, topics): await connection.close() return @@ -108,10 +119,7 @@ class Server: cls._logger.exception(f'Ошибка WebSocket подключения: {e}') @classmethod - async def _process_headers(cls, connection: WebSocket) -> bool: - headers = connection.headers - token = headers.get('Authorization') - topics = headers.get('Subscribe') + async def _process_query_params(cls, connection: WebSocket, token: str, topics: str) -> bool: if token not in cls._static_tokens and token not in cls._temp_tokens: return False