实时语音聊天和文本对话的WebSocket连接端点
// 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" }