feat(reverse_ws): 添加反向WebSocket支持及负载均衡功能
- 新增反向WebSocket管理器模块,支持多客户端连接 - 实现负载均衡机制,自动选择健康且负载最低的客户端 - 添加防重复事件处理机制,防止消息重复处理 - 更新配置模型和加载器以支持反向WebSocket配置 - 添加示例文件和文档说明使用方法 - 修改主程序启动逻辑以支持反向WebSocket服务
This commit is contained in:
58
examples/reverse_ws_example.py
Normal file
58
examples/reverse_ws_example.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
反向 WebSocket 使用示例
|
||||
|
||||
该文件展示了如何使用反向 WebSocket 功能。
|
||||
"""
|
||||
|
||||
from core.managers import reverse_ws_manager
|
||||
|
||||
|
||||
async def example_usage():
|
||||
"""
|
||||
使用示例
|
||||
"""
|
||||
# 1. 启动反向 WebSocket 服务端
|
||||
await reverse_ws_manager.start(host="0.0.0.0", port=3002)
|
||||
|
||||
# 2. 等待客户端连接
|
||||
# 此时 OneBot 实现(如 NapCat)应该连接到 ws://your-server-ip:3002
|
||||
|
||||
# 3. 查看已连接的客户端
|
||||
connected_clients = reverse_ws_manager.get_connected_clients()
|
||||
print(f"已连接的客户端: {connected_clients}")
|
||||
|
||||
# 4. 查看健康的客户端
|
||||
healthy_clients = reverse_ws_manager.get_healthy_clients()
|
||||
print(f"健康的客户端: {healthy_clients}")
|
||||
|
||||
# 5. 调用 API(使用负载均衡)
|
||||
response = await reverse_ws_manager.call_api(
|
||||
action="get_login_info",
|
||||
params={},
|
||||
use_load_balance=True # 启用负载均衡
|
||||
)
|
||||
print(f"API 响应: {response}")
|
||||
|
||||
# 6. 调用 API(向特定客户端发送)
|
||||
if connected_clients:
|
||||
client_id = list(connected_clients.keys())[0]
|
||||
response = await reverse_ws_manager.call_api(
|
||||
action="get_login_info",
|
||||
params={},
|
||||
client_id=client_id,
|
||||
use_load_balance=False # 不使用负载均衡
|
||||
)
|
||||
print(f"特定客户端 API 响应: {response}")
|
||||
|
||||
# 7. 获取负载最低的客户端
|
||||
least_load_client = reverse_ws_manager.get_client_with_least_load()
|
||||
if least_load_client:
|
||||
print(f"负载最低的客户端: {least_load_client}")
|
||||
|
||||
# 8. 停止服务端
|
||||
await reverse_ws_manager.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
asyncio.run(example_usage())
|
||||
Reference in New Issue
Block a user