ruff
This commit is contained in:
65
core/ws.py
65
core/ws.py
@@ -1,13 +1,17 @@
|
||||
import asyncio
|
||||
import json
|
||||
import uuid
|
||||
import websockets
|
||||
import traceback
|
||||
from .command_manager import matcher
|
||||
from .config_loader import global_config
|
||||
from models import Event
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
import websockets
|
||||
|
||||
from models import Event
|
||||
|
||||
from .command_manager import matcher
|
||||
from .config_loader import global_config
|
||||
|
||||
|
||||
class WS:
|
||||
def __init__(self):
|
||||
# 读取参数
|
||||
@@ -15,28 +19,33 @@ class WS:
|
||||
self.url = cfg.get("uri")
|
||||
self.token = cfg.get("token")
|
||||
self.reconnect_interval = cfg.get("reconnect_interval", 5)
|
||||
|
||||
self.ws = None
|
||||
self._pending_requests = {}
|
||||
|
||||
self.ws = None
|
||||
self._pending_requests = {}
|
||||
|
||||
async def connect(self):
|
||||
"""主连接循环"""
|
||||
headers = {"Authorization": f"Bearer {self.token}"} if self.token else {}
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
print(f" 正在尝试连接至 NapCat: {self.url}")
|
||||
async with websockets.connect(self.url, additional_headers=headers) as websocket:
|
||||
async with websockets.connect(
|
||||
self.url, additional_headers=headers
|
||||
) as websocket:
|
||||
self.ws = websocket
|
||||
print(" 连接成功!")
|
||||
await self._listen_loop(websocket)
|
||||
|
||||
except (websockets.exceptions.ConnectionClosed, ConnectionRefusedError) as e:
|
||||
|
||||
except (
|
||||
websockets.exceptions.ConnectionClosed,
|
||||
ConnectionRefusedError,
|
||||
) as e:
|
||||
print(f" 连接断开或服务器拒绝访问: {e}")
|
||||
except Exception as e:
|
||||
print(f" 运行异常: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
print(f" {self.reconnect_interval}秒后尝试重连...")
|
||||
await asyncio.sleep(self.reconnect_interval)
|
||||
|
||||
@@ -45,20 +54,20 @@ class WS:
|
||||
async for message in websocket:
|
||||
try:
|
||||
data = json.loads(message)
|
||||
|
||||
|
||||
# 1. 处理 API 响应
|
||||
echo_id = data.get("echo")
|
||||
if echo_id and echo_id in self._pending_requests:
|
||||
future = self._pending_requests.pop(echo_id)
|
||||
if not future.done():
|
||||
future.set_result(data)
|
||||
continue
|
||||
continue
|
||||
|
||||
# 2. 处理上报事件
|
||||
if "post_type" in data:
|
||||
# 使用 create_task 异步执行,避免阻塞
|
||||
asyncio.create_task(self.on_event(data))
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f" 解析消息异常: {e}")
|
||||
|
||||
@@ -67,7 +76,7 @@ class WS:
|
||||
try:
|
||||
# 解析为 Event 对象
|
||||
event = Event.from_dict(raw_data)
|
||||
|
||||
|
||||
# 格式化时间用于打印
|
||||
t = datetime.fromtimestamp(event.time).strftime("%H:%M:%S")
|
||||
|
||||
@@ -75,12 +84,16 @@ class WS:
|
||||
|
||||
# A. 消息事件 (Message)
|
||||
if event.post_type == "message":
|
||||
print(f" [{t}] [消息] {event.message_type} | {event.user_id}: {event.raw_message}")
|
||||
print(
|
||||
f" [{t}] [消息] {event.message_type} | {event.user_id}: {event.raw_message}"
|
||||
)
|
||||
await matcher.handle_message(self, event)
|
||||
|
||||
# B. 通知事件 (Notice)
|
||||
elif event.post_type == "notice":
|
||||
print(f" [{t}] [通知] {event.notice_type} | 来自: {event.group_id or '私聊'}")
|
||||
print(
|
||||
f" [{t}] [通知] {event.notice_type} | 来自: {event.group_id or '私聊'}"
|
||||
)
|
||||
await matcher.handle_notice(self, event)
|
||||
|
||||
# C. 请求事件 (Request)
|
||||
@@ -100,23 +113,23 @@ class WS:
|
||||
"""调用 OneBot API"""
|
||||
if not self.ws:
|
||||
return {"status": "failed", "msg": "websocket not initialized"}
|
||||
|
||||
|
||||
from websockets.protocol import State
|
||||
|
||||
if getattr(self.ws, "state", None) is not State.OPEN:
|
||||
return {"status": "failed", "msg": "websocket is not open"}
|
||||
return {"status": "failed", "msg": "websocket is not open"}
|
||||
|
||||
echo_id = str(uuid.uuid4())
|
||||
payload = {"action": action, "params": params or {}, "echo": echo_id}
|
||||
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
future = loop.create_future()
|
||||
self._pending_requests[echo_id] = future
|
||||
|
||||
|
||||
await self.ws.send(json.dumps(payload))
|
||||
|
||||
|
||||
try:
|
||||
|
||||
return await asyncio.wait_for(future, timeout=30.0)
|
||||
except asyncio.TimeoutError:
|
||||
self._pending_requests.pop(echo_id, None)
|
||||
return {"status": "failed", "retcode": -1, "msg": "api timeout"}
|
||||
return {"status": "failed", "retcode": -1, "msg": "api timeout"}
|
||||
|
||||
Reference in New Issue
Block a user