Files
xiaozhi-esp32-server/main/xiaozhi-server/core/websocket_server.py
T

85 lines
3.7 KiB
Python
Raw Normal View History

2025-02-02 23:01:14 +08:00
import asyncio
import websockets
2025-02-18 00:07:19 +08:00
from config.logger import setup_logging
2025-02-02 23:01:14 +08:00
from core.connection import ConnectionHandler
from core.utils.util import get_local_ip
2025-03-09 21:33:45 +08:00
from core.utils import asr, vad, llm, tts, memory, intent
2025-02-02 23:01:14 +08:00
2025-02-18 00:07:19 +08:00
TAG = __name__
2025-02-02 23:01:14 +08:00
2025-02-23 14:38:21 +08:00
2025-02-02 23:01:14 +08:00
class WebSocketServer:
def __init__(self, config: dict):
self.config = config
2025-02-18 00:07:19 +08:00
self.logger = setup_logging()
2025-03-17 02:13:10 +08:00
self._vad, self._asr, self._llm, self._tts, self._memory, self.intent = self._create_processing_instances()
2025-02-26 01:33:05 +08:00
self.active_connections = set() # 添加全局连接记录
2025-02-02 23:01:14 +08:00
def _create_processing_instances(self):
2025-03-09 21:33:45 +08:00
memory_cls_name = self.config["selected_module"].get("Memory", "nomem") # 默认使用nomem
2025-03-03 15:00:04 +08:00
has_memory_cfg = self.config.get("Memory") and memory_cls_name in self.config["Memory"]
memory_cfg = self.config["Memory"][memory_cls_name] if has_memory_cfg else {}
2025-02-02 23:01:14 +08:00
"""创建处理模块实例"""
return (
vad.create_instance(
self.config["selected_module"]["VAD"],
self.config["VAD"][self.config["selected_module"]["VAD"]]
),
asr.create_instance(
2025-02-23 14:38:21 +08:00
self.config["selected_module"]["ASR"]
if not 'type' in self.config["ASR"][self.config["selected_module"]["ASR"]]
else
self.config["ASR"][self.config["selected_module"]["ASR"]]["type"],
2025-02-02 23:01:14 +08:00
self.config["ASR"][self.config["selected_module"]["ASR"]],
self.config["delete_audio"]
),
llm.create_instance(
self.config["selected_module"]["LLM"]
if not 'type' in self.config["LLM"][self.config["selected_module"]["LLM"]]
else
self.config["LLM"][self.config["selected_module"]["LLM"]]['type'],
2025-02-02 23:01:14 +08:00
self.config["LLM"][self.config["selected_module"]["LLM"]],
),
tts.create_instance(
self.config["selected_module"]["TTS"]
if not 'type' in self.config["TTS"][self.config["selected_module"]["TTS"]]
else
self.config["TTS"][self.config["selected_module"]["TTS"]]["type"],
2025-02-02 23:01:14 +08:00
self.config["TTS"][self.config["selected_module"]["TTS"]],
self.config["delete_audio"]
2025-02-26 01:33:05 +08:00
),
2025-03-03 15:00:04 +08:00
memory.create_instance(memory_cls_name, memory_cfg),
2025-03-09 21:33:45 +08:00
intent.create_instance(
self.config["selected_module"]["Intent"]
if not 'type' in self.config["Intent"][self.config["selected_module"]["Intent"]]
else
self.config["Intent"][self.config["selected_module"]["Intent"]]["type"],
self.config["Intent"][self.config["selected_module"]["Intent"]]
),
2025-02-02 23:01:14 +08:00
)
async def start(self):
server_config = self.config["server"]
host = server_config["ip"]
port = server_config["port"]
2025-02-18 00:07:19 +08:00
self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port)
self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======")
2025-02-02 23:01:14 +08:00
async with websockets.serve(
self._handle_connection,
host,
port
):
await asyncio.Future()
async def _handle_connection(self, websocket):
"""处理新连接,每次创建独立的ConnectionHandler"""
2025-02-26 01:33:05 +08:00
# 创建ConnectionHandler时传入当前server实例
2025-03-17 02:13:10 +08:00
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts, self._memory, self.intent)
2025-02-26 01:33:05 +08:00
self.active_connections.add(handler)
try:
await handler.handle_connection(websocket)
finally:
self.active_connections.discard(handler)