← 返回API文档

🔌 WebSocket接口文档

WS 语音聊天WebSocket接口
ws://your-domain.com/ws/speech

实时语音聊天和文本对话的WebSocket连接端点

📋 接口说明

🎤 语音功能

  • 实时音频流传输
  • 语音识别和转换
  • 音频质量优化
  • 多语言支持

💬 文本对话

  • 实时文本消息
  • JSON格式数据
  • 消息历史记录
  • 上下文理解

🔄 连接管理

  • 自动重连机制
  • 连接状态监控
  • 资源自动清理
  • 错误处理

🚀 连接示例

// JavaScript客户端示例
const ws = new WebSocket('ws://your-domain.com/ws/speech');

// 连接建立
ws.onopen = function(event) {
    console.log('WebSocket连接已建立');
    // 发送文本消息
    ws.send(JSON.stringify({
        type: 'text',
        message: '你好,我想开始对话'
    }));
};

// 接收消息
ws.onmessage = function(event) {
    const data = JSON.parse(event.data);
    console.log('收到AI回复:', data.message);
};

// 连接关闭
ws.onclose = function(event) {
    console.log('WebSocket连接已关闭');
};

// 错误处理
ws.onerror = function(error) {
    console.error('WebSocket错误:', error);
};

📝 消息格式

发送消息格式:

// 文本消息
{
    "type": "text",
    "message": "用户输入的文本内容",
    "timestamp": "2025-10-15T13:30:00Z"
}

// 音频消息
{
    "type": "audio",
    "data": "base64编码的音频数据",
    "format": "wav",
    "sampleRate": 16000
}

接收消息格式:

// AI回复消息
{
    "type": "response",
    "message": "AI的回复内容",
    "timestamp": "2025-10-15T13:30:01Z",
    "confidence": 0.95
}

// 系统消息
{
    "type": "system",
    "message": "连接已建立",
    "status": "connected"
}

🔧 技术规格

⚠️ 注意事项