-
-
-
-
-
-
会话记录
-
-
-
-
准备就绪,请连接服务器开始测试...
+
+
+
-
-
+
-
- // DOM元素
- const connectButton = document.getElementById('connectButton');
- const serverUrlInput = document.getElementById('serverUrl');
- const connectionStatus = document.getElementById('connectionStatus');
- const messageInput = document.getElementById('messageInput');
- const sendTextButton = document.getElementById('sendTextButton');
- const recordButton = document.getElementById('recordButton');
- const stopButton = document.getElementById('stopButton');
- const conversationDiv = document.getElementById('conversation');
- const logContainer = document.getElementById('logContainer');
+
+ // 编码并发送剩余的数据
+ encodeAndSendOpus();
+
+ // 发送一个空的消息作为结束标志(模拟接收到空音频数据的情况)
+ if (websocket && websocket.readyState === WebSocket.OPEN) {
+ // 使用空的Uint8Array发送最后一个空帧
+ const emptyOpusFrame = new Uint8Array(0);
+ websocket.send(emptyOpusFrame);
+
+ // 发送监听结束消息
+ const stopMessage = {
+ type: 'listen',
+ mode: 'manual',
+ state: 'stop'
+ };
+
+ websocket.send(JSON.stringify(stopMessage));
+ log('已发送录音停止信号', 'info');
+ }
+
+ // 重置UI
+ recordButton.textContent = '开始录音';
+ recordButton.classList.remove('recording');
+ recordButton.disabled = false;
+
+ log('停止PCM直接录音', 'success');
+ return true;
+ } catch (error) {
+ log(`直接录音停止错误: ${error.message}`, 'error');
+ return false;
+ }
+ }
+
+ async function handleBinaryMessage(data) {
+ try {
+ let arrayBuffer;
+ // 根据数据类型进行处理
+ if (data instanceof ArrayBuffer) {
+ arrayBuffer = data;
+ log(`收到ArrayBuffer音频数据,大小: ${data.byteLength}字节`, 'debug');
+ } else if (data instanceof Blob) {
+ // 如果是Blob类型,转换为ArrayBuffer
+ arrayBuffer = await data.arrayBuffer();
+ log(`收到Blob音频数据,大小: ${arrayBuffer.byteLength}字节`, 'debug');
+ } else {
+ log(`收到未知类型的二进制数据: ${typeof data}`, 'warning');
+ return;
+ }
+ // 创建Uint8Array用于处理
+ const opusData = new Uint8Array(arrayBuffer);
+ if (opusData.length > 0) {
+ // 将数据添加到缓冲队列
+ queue.enqueue(opusData);
+ } else {
+ log('收到空音频数据帧,可能是结束标志', 'warning');
+ // 如果正在播放,发送结束信号
+ if (isAudioPlaying && streamingContext) {
+ streamingContext.endOfStream = true;
+ }
+ }
+ } catch (error) {
+ log(`处理二进制消息出错: ${error.message}`, 'error');
+ }
+ }
+
+ // 获取配置值
+ function getConfig() {
+ const deviceMac = document.getElementById('deviceMac').value.trim();
+ return {
+ deviceId: deviceMac, // 使用MAC地址作为deviceId
+ deviceName: document.getElementById('deviceName').value.trim(),
+ deviceMac: deviceMac,
+ clientId: document.getElementById('clientId').value.trim(),
+ token: document.getElementById('token').value.trim()
+ };
+ }
+
+ initApp();
+