fix(wecomapp): 修复WebSocket连接和事件处理器问题

修复企业微信机器人无法正常连接的问题:

1. 修复WSClient初始化参数传递错误
   - 原代码错误地将参数传递给WSClient构造函数
   - 修正为正确传递host, port, path等参数

2. 修复连接方法调用错误
   - 将connect_async()改为connect()方法
   - AiBotSDK的WSClient使用同步connect方法

3. 修复事件处理器签名不匹配
   - on_connected/on_authenticated: 移除frame参数(这些处理器不需要参数)
   - on_disconnected: 添加reason参数
   - on_error: 添加error参数

修复后验证:
- WebSocket连接成功建立
- 认证过程正常完成
- 心跳机制正常工作
- 日志无错误信息

此修复解决了企业微信机器人启动后无法连接服务器的问题。
This commit is contained in:
郭春飞
2026-04-25 01:18:28 +08:00
parent 114dfdb211
commit 936501069c

View File

@@ -71,20 +71,20 @@ class WeComApp(AgentChatMixin):
except Exception as e:
print(f"[WeCom] welcome error: {e}")
async def on_connected(self, frame):
async def on_connected(self):
print("[WeCom] connected")
async def on_authenticated(self, frame):
async def on_authenticated(self):
print("[WeCom] authenticated")
async def on_disconnected(self, frame):
print("[WeCom] disconnected")
async def on_disconnected(self, reason=""):
print(f"[WeCom] disconnected: {reason}")
async def on_error(self, frame):
print(f"[WeCom] error: {frame}")
async def on_error(self, error=None):
print(f"[WeCom] error: {error}")
async def start(self):
self.client = WSClient({"bot_id": BOT_ID, "secret": SECRET, "reconnect_interval": 1000, "max_reconnect_attempts": -1, "heartbeat_interval": 30000})
self.client = WSClient(BOT_ID, SECRET, reconnect_interval=1000, max_reconnect_attempts=-1, heartbeat_interval=30000)
for event, handler in {
"connected": self.on_connected,
"authenticated": self.on_authenticated,
@@ -95,7 +95,7 @@ class WeComApp(AgentChatMixin):
}.items():
self.client.on(event, handler)
print("[WeCom] bot starting...")
await self.client.connect_async()
await self.client.connect()
while True:
await asyncio.sleep(1)