diff --git a/server.js b/server.js index 96e519a..03a047b 100644 --- a/server.js +++ b/server.js @@ -9,8 +9,8 @@ const { MessageHistory } = require('./src/message_history.js'); const app = express(); const server = http.createServer(app); const io = socketIo(server, { - pingTimeout: 300000, // 60秒超时 - pingInterval: 25000, // 25秒心跳间隔 + pingTimeout: 60000, // 60秒超时 (减少到1分钟) + pingInterval: 10000, // 10秒心跳间隔 upgradeTimeout: 30000, // 30秒升级超时 allowEIO3: true, // 允许Engine.IO v3客户端 transports: ['websocket', 'polling'], // 支持多种传输方式 @@ -137,8 +137,7 @@ const scenes = [ tag: 'chat', apiKey: 'bot-20250916100919-w8vxr', // 起床场景的API key openingLines: [ - "嗨,我是最可爱的小男孩,小乐。", - "爷爷奶奶,在干嘛呢?", + " 爷爷/奶奶,我来啦!今天您过得怎么样呀?有没有什么好玩的事儿跟我说说呀?", ] } ]; diff --git a/src/index.js b/src/index.js index 0b01863..dfe7314 100644 --- a/src/index.js +++ b/src/index.js @@ -33,6 +33,9 @@ class WebRTCChat { this.isVideoReady = false; this.isDefaultVideoLoaded = false; this.retryCount = 0; // 添加重试计数器 + this.connectionRetryCount = 0; // 连接重试计数器 + this.maxRetryAttempts = 5; // 最大重试次数 + this.reconnectTimeout = null; // 重连定时器 // 添加视频相关属性 this.videoSender = null; // WebRTC视频发送器 @@ -172,16 +175,37 @@ class WebRTCChat { } initializeSocket() { - this.socket = io(); + this.socket = io({ + // 客户端配置,与服务器端保持一致 + pingTimeout: 60000, + pingInterval: 10000, + upgradeTimeout: 30000, + transports: ['websocket', 'polling'] + }); this.socket.on('connect', () => { this.updateStatus('已连接到服务器', 'connected'); this.logMessage('已连接到服务器', 'success'); + this.connectionRetryCount = 0; // 重置重连计数 }); - this.socket.on('disconnect', () => { + this.socket.on('disconnect', (reason) => { this.connectionStatus.style.display = 'none'; - this.logMessage('与服务器断开连接', 'error'); + this.logMessage(`与服务器断开连接: ${reason}`, 'error'); + + // 自动重连机制 + if (reason === 'io server disconnect') { + // 服务器主动断开,不自动重连 + this.logMessage('服务器主动断开连接', 'warning'); + } else { + // 网络问题或其他原因,尝试重连 + this.attemptReconnect(); + } + }); + + this.socket.on('connect_error', (error) => { + this.logMessage(`连接错误: ${error.message}`, 'error'); + this.attemptReconnect(); }); // WebRTC 信令处理 @@ -1398,6 +1422,12 @@ class WebRTCChat { // 清理视频缓存和预创建流 this.clearVideoCache(); + // 清理重连定时器 + if (this.reconnectTimeout) { + clearTimeout(this.reconnectTimeout); + this.reconnectTimeout = null; + } + // setTimeout(() => { // // 显示头像,隐藏视频 // if (this.videoContainer) { @@ -1829,6 +1859,36 @@ class WebRTCChat { // 检查当前视频流状态 this.checkVideoStreamStatus(); } + + // 尝试重连 + attemptReconnect() { + if (this.connectionRetryCount >= this.maxRetryAttempts) { + this.logMessage('已达到最大重连次数,停止重连', 'error'); + return; + } + + this.connectionRetryCount++; + const delay = Math.min(1000 * Math.pow(2, this.connectionRetryCount - 1), 30000); // 指数退避,最大30秒 + + this.logMessage(`尝试重连 (${this.connectionRetryCount}/${this.maxRetryAttempts}),${delay/1000}秒后重试...`, 'warning'); + + this.reconnectTimeout = setTimeout(() => { + if (this.socket && !this.socket.connected) { + this.logMessage('正在重新连接...', 'info'); + this.socket.connect(); + } + }, delay); + } + + // 检查连接状态 + checkConnectionStatus() { + if (!this.socket || !this.socket.connected) { + this.logMessage('检测到连接断开,尝试重连...', 'warning'); + this.attemptReconnect(); + return false; + } + return true; + } } // 页面加载完成后初始化应用 diff --git a/src/minimaxi_stream.js b/src/minimaxi_stream.js index 1f7cb09..0703758 100644 --- a/src/minimaxi_stream.js +++ b/src/minimaxi_stream.js @@ -70,6 +70,12 @@ async function processAudioQueue() { // 如果是第一个音频片段,触发视频切换 if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.switchVideoStream) { try { + // 检查WebSocket连接状态 + if (window.webrtcApp.checkConnectionStatus && !window.webrtcApp.checkConnectionStatus()) { + console.log('WebSocket连接异常,跳过视频切换'); + return; + } + console.log('--------------触发视频切换:', sayName); window.webrtcApp.switchVideoStream(targetVideo, 'audio', '8-4-sh'); isFirstChunk = false; diff --git a/videos/xnh-bd-2.mp4 b/videos/xnh-bd-2.mp4 index 1affa52..ff180b0 100644 Binary files a/videos/xnh-bd-2.mp4 and b/videos/xnh-bd-2.mp4 differ diff --git a/videos/xnh-sh.mp4 b/videos/xnh-sh.mp4 index 9a44d86..28b3cd6 100644 Binary files a/videos/xnh-sh.mp4 and b/videos/xnh-sh.mp4 differ