diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index e23826a1..9f1348b7 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -282,14 +282,13 @@ class ConnectionHandler: if self.asr is None: return - # 检查是否需要处理头部(只有当mqtt_gateway有实际值时才处理头部) - mqtt_gateway = self.config.get("server", {}).get("mqtt_gateway") - # 当mqtt_gateway为None, "null", "", 或实际的null值时,不处理头部 - need_header_processing = mqtt_gateway and mqtt_gateway not in [None, "null", ""] and str(mqtt_gateway).strip() != "" + # 检查是否需要处理头部(只有当websocket URL以"?from=mqtt"为结尾时才处理头部) + request_path = self.websocket.request.path + need_header_processing = request_path.endswith("?from=mqtt") # 调试日志:首次连接时记录配置 if not hasattr(self, '_logged_mqtt_config'): - self.logger.bind(tag=TAG).info(f"MQTT Gateway配置: '{mqtt_gateway}', 头部处理: {need_header_processing}") + self.logger.bind(tag=TAG).info(f"WebSocket URL路径: '{request_path}', 头部处理: {need_header_processing}") self._logged_mqtt_config = True if need_header_processing and len(message) >= 16: diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 56d21932..df18f2a0 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -42,11 +42,9 @@ async def sendAudio(conn, audios, frame_duration=60): """ if audios is None or len(audios) == 0: return - - # 检查是否需要添加头部(只有当mqtt_gateway有实际值时才添加头部) - mqtt_gateway = conn.config.get("server", {}).get("mqtt_gateway") - # 当mqtt_gateway为None, "null", "", 或实际的null值时,不添加头部 - need_header = mqtt_gateway and mqtt_gateway not in [None, "null", ""] and str(mqtt_gateway).strip() != "" + # 检查是否需要处理头部(只有当websocket URL以"?from=mqtt"为结尾时才处理头部) + request_path = conn.websocket.request.path + need_header = request_path.endswith("?from=mqtt") if isinstance(audios, bytes): if conn.client_abort: @@ -99,10 +97,9 @@ async def sendAudio(conn, audios, frame_duration=60): start_time = time.perf_counter() play_position = 0 - # 检查是否需要添加头部(只有当mqtt_gateway有实际值时才添加头部) - mqtt_gateway = conn.config.get("server", {}).get("mqtt_gateway") - # 当mqtt_gateway为None, "null", "", 或实际的null值时,不添加头部 - need_header = mqtt_gateway and mqtt_gateway not in [None, "null", ""] and str(mqtt_gateway).strip() != "" + # 检查是否需要添加头部(只有当websocket URL以"?from=mqtt"为结尾时才添加头部) + request_path = conn.websocket.request.path + need_header = request_path.endswith("?from=mqtt") # 执行预缓冲 pre_buffer_frames = min(3, len(audios))