From 1e09b72404d655c267a283008b487a553a91c793 Mon Sep 17 00:00:00 2001 From: rassomakhin Date: Wed, 8 Oct 2025 08:56:31 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wsServ.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 wsServ.py diff --git a/wsServ.py b/wsServ.py new file mode 100644 index 0000000..53e9ad2 --- /dev/null +++ b/wsServ.py @@ -0,0 +1,45 @@ +import asyncio +import websockets +import json + +async def handler(websocket, path): + print(f"Новое подключение на путь: {path}") + try: + async for message in websocket: + print(f"Получено от {path}: {message}") + data = json.loads(message) + message = json.dumps(data, ensure_ascii=False) + +# if data["count_message"] == 3 or data["count_message"] == 7: +# for i in range(2): +# data["count_message"] = data["count_message"] + 1 +# message = json.dumps(data, ensure_ascii=False) +# await websocket.send(message) +# elif data["count_message"] == 6: +# continue + + data["count_message"] = data["count_message"] + 1 + data["orm_peer"] = "orm.local" + data["orm_version"] = "2.1" + data["orm_opc_client_peer"] = "opc.orm.local" + if data["status"] == "init_INIT_AAA": + data["status"] = "init_OK_AAA" + elif data["status"] == "init_INIT_LOCATION": + data["status"] = "init_OK_LOCATION" + elif data["status"] == "init_INIT_CHAIN": + data["status"] = "init_OK_CHAIN" + message = json.dumps(data, ensure_ascii=False) + print(f"Отправлено на {path}: {message}") + await websocket.send(message) + except websockets.ConnectionClosed: + print(f"Подключение на {path} закрыто") + + + +async def main(): + async with websockets.serve(handler, "localhost", 8080): + print("WebSocket сервер запущен на ws://localhost:8080/") + await asyncio.Future() + +if __name__ == "__main__": + asyncio.run(main())