mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
- Defaults and config aligned with English-first product surface - Adds/removes ASR/TTS paths; drops China-only provider implementations and bundled SenseVoice assets - Westernizes prompts, plugin/tool copy, connection and handler behavior on upstream fixes - Powermem max_tokens and OpenAI no-thinking guard preserved on downstream path
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import json
|
|
import time
|
|
from typing import Dict, Any
|
|
|
|
from core.handle.textMessageHandler import TextMessageHandler
|
|
from core.handle.textMessageType import TextMessageType
|
|
|
|
TAG = __name__
|
|
|
|
|
|
class PingMessageHandler(TextMessageHandler):
|
|
"""Ping handler used to keep the WebSocket connection alive."""
|
|
|
|
@property
|
|
def message_type(self) -> TextMessageType:
|
|
return TextMessageType.PING
|
|
|
|
async def handle(self, conn, msg_json: Dict[str, Any]) -> None:
|
|
"""
|
|
Handle a PING message and send a PONG response.
|
|
|
|
Args:
|
|
conn: WebSocket connection object
|
|
msg_json: JSON payload for the PING message
|
|
"""
|
|
# Ignore heartbeat traffic when WebSocket ping handling is disabled.
|
|
enable_websocket_ping = conn.config.get("enable_websocket_ping", False)
|
|
if not enable_websocket_ping:
|
|
conn.logger.debug("WebSocket heartbeat handling is disabled; ignoring PING")
|
|
return
|
|
|
|
try:
|
|
conn.logger.debug("Received PING message, sending PONG response")
|
|
conn.last_activity_time = time.time() * 1000
|
|
# Build the PONG response payload.
|
|
pong_message = {
|
|
"type": "pong",
|
|
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
|
}
|
|
|
|
# Send the PONG response.
|
|
await conn.websocket.send(json.dumps(pong_message))
|
|
|
|
except Exception as e:
|
|
conn.logger.error(f"Error while handling PING message: {e}")
|