2025-02-02 23:01:14 +08:00
|
|
|
import logging
|
2025-02-04 13:57:05 +08:00
|
|
|
import json
|
|
|
|
|
from core.handle.abortHandle import handleAbortMessage
|
|
|
|
|
from core.handle.helloHandle import handleHelloMessage
|
|
|
|
|
from core.handle.audioHandle import startToChat
|
2025-02-02 23:01:14 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def handleTextMessage(conn, message):
|
2025-02-04 13:57:05 +08:00
|
|
|
"""处理文本消息"""
|
|
|
|
|
logger.info(f"收到文本消息:{message}")
|
|
|
|
|
try:
|
|
|
|
|
msg_json = json.loads(message)
|
|
|
|
|
if msg_json["type"] == "hello":
|
2025-02-06 12:30:26 +08:00
|
|
|
await handleHelloMessage(conn)
|
2025-02-04 13:57:05 +08:00
|
|
|
elif msg_json["type"] == "abort":
|
|
|
|
|
await handleAbortMessage(conn)
|
|
|
|
|
elif msg_json["type"] == "listen":
|
|
|
|
|
if "mode" in msg_json:
|
|
|
|
|
conn.client_listen_mode = msg_json["mode"]
|
2025-02-04 18:38:15 +08:00
|
|
|
logger.debug(f"客户端拾音模式:{conn.client_listen_mode}")
|
2025-02-04 13:57:05 +08:00
|
|
|
if msg_json["state"] == "start":
|
|
|
|
|
conn.client_have_voice = True
|
|
|
|
|
conn.client_voice_stop = False
|
|
|
|
|
elif msg_json["state"] == "stop":
|
|
|
|
|
conn.client_have_voice = True
|
|
|
|
|
conn.client_voice_stop = True
|
|
|
|
|
elif msg_json["state"] == "detect":
|
|
|
|
|
conn.asr_server_receive = False
|
|
|
|
|
conn.client_have_voice = False
|
|
|
|
|
conn.asr_audio.clear()
|
|
|
|
|
if "text" in msg_json:
|
|
|
|
|
await startToChat(conn, msg_json["text"])
|
|
|
|
|
except json.JSONDecodeError:
|
2025-02-06 12:30:26 +08:00
|
|
|
await conn.websocket.send(message)
|