feat: Добавлен метод send
This commit is contained in:
@@ -49,6 +49,17 @@ class Server:
|
|||||||
|
|
||||||
return request
|
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
|
@classmethod
|
||||||
def _make_request(cls):
|
def _make_request(cls):
|
||||||
request_id = str(uuid4())
|
request_id = str(uuid4())
|
||||||
@@ -65,8 +76,8 @@ class Server:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def _register_routes(cls, app):
|
def _register_routes(cls, app):
|
||||||
@app.websocket('/pyg2o')
|
@app.websocket('/pyg2o')
|
||||||
async def pyg2o(websocket: WebSocket):
|
async def pyg2o(websocket: WebSocket, token: str, topics: str):
|
||||||
await cls._handle_connection(websocket)
|
await cls._handle_connection(websocket, token, topics)
|
||||||
|
|
||||||
_ = pyg2o
|
_ = pyg2o
|
||||||
|
|
||||||
@@ -85,9 +96,9 @@ class Server:
|
|||||||
cls._topics[topic].discard(connection)
|
cls._topics[topic].discard(connection)
|
||||||
|
|
||||||
@classmethod
|
@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()
|
await connection.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -108,10 +119,7 @@ class Server:
|
|||||||
cls._logger.exception(f'Ошибка WebSocket подключения: {e}')
|
cls._logger.exception(f'Ошибка WebSocket подключения: {e}')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def _process_headers(cls, connection: WebSocket) -> bool:
|
async def _process_query_params(cls, connection: WebSocket, token: str, topics: str) -> bool:
|
||||||
headers = connection.headers
|
|
||||||
token = headers.get('Authorization')
|
|
||||||
topics = headers.get('Subscribe')
|
|
||||||
|
|
||||||
if token not in cls._static_tokens and token not in cls._temp_tokens:
|
if token not in cls._static_tokens and token not in cls._temp_tokens:
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user