forked from HornCopper/Lagrange-Python.OneBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.py
69 lines (59 loc) · 2.67 KB
/
ws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import websockets
import json
import asyncio
from config import logger, Config
from lagrange.client.client import Client
from onebot.communications.api import OneBotAPI_V11
websocket_connection = None
async def process(client: Client, data: dict, instance: OneBotAPI_V11) -> dict:
echo = data.get("echo", "")
action: str = data.get("action", "")
if not hasattr(instance, action):
logger.onebot.error(f"Client Request Action Failed: `{action}` Not Exists.")
return {"status": "failed", "retcode": -1, "data": None, "echo": echo}
params: dict = data.get("params", {})
logger.onebot.debug(
f"Client Request Action Successfully: `{action}` with echo {echo}, params: " + \
json.dumps(
params,
ensure_ascii=False
)
)
method = getattr(instance, action)
try:
resp = await method(echo=echo, **params)
logger.onebot.debug(f"Send Response: `{resp}`.")
except Exception as e:
logger.onebot.error(f"Error when calling `{action}`: {e}")
return {"status": "failed", "retcode": -1, "data": None, "echo": echo}
return resp
async def connect(client: Client):
global websocket_connection
uri = Config.ws_url
instance = OneBotAPI_V11(client=client)
if not Config.ws_url.startswith("ws://") and not Config.ws_url.startswith("wss://"):
return
while True:
try:
async with websockets.connect(
uri,
extra_headers={"X-Self-Id": str(client.uin)}, max_size=10*1024*1024
) as websocket:
websocket_connection = websocket
logger.onebot.success(f"Reserved WebSocket Connected to {Config.ws_url}!")
while True:
try:
rec = await websocket.recv()
rec = json.loads(rec)
rply = await process(client, rec, instance)
await websocket.send(json.dumps(rply, ensure_ascii=False))
except websockets.exceptions.ConnectionClosed as e:
logger.onebot.warning(f"Reserved WebSocket Closed, status code: {e.code}")
break
except Exception as e:
logger.onebot.error(f"Unhandled Exception in message handling: {e}")
except (websockets.exceptions.ConnectionClosed, websockets.exceptions.ConnectionClosedError) as e:
logger.onebot.warning(f"Reserved WebSocket Connection Closed: {e}, retrying...")
except ConnectionRefusedError:
logger.onebot.error("Reserved WebSocket Connection Refused, retrying...")
await asyncio.sleep(5)