修改视频、开场白
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 21s

This commit is contained in:
Song367 2025-09-17 13:27:03 +08:00
parent 9272d49e73
commit d19f53ec6e
5 changed files with 72 additions and 7 deletions

View File

@ -9,8 +9,8 @@ const { MessageHistory } = require('./src/message_history.js');
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
const io = socketIo(server, { const io = socketIo(server, {
pingTimeout: 300000, // 60秒超时 pingTimeout: 60000, // 60秒超时 (减少到1分钟)
pingInterval: 25000, // 25秒心跳间隔 pingInterval: 10000, // 10秒心跳间隔
upgradeTimeout: 30000, // 30秒升级超时 upgradeTimeout: 30000, // 30秒升级超时
allowEIO3: true, // 允许Engine.IO v3客户端 allowEIO3: true, // 允许Engine.IO v3客户端
transports: ['websocket', 'polling'], // 支持多种传输方式 transports: ['websocket', 'polling'], // 支持多种传输方式
@ -137,8 +137,7 @@ const scenes = [
tag: 'chat', tag: 'chat',
apiKey: 'bot-20250916100919-w8vxr', // 起床场景的API key apiKey: 'bot-20250916100919-w8vxr', // 起床场景的API key
openingLines: [ openingLines: [
"嗨,我是最可爱的小男孩,小乐。", " 爷爷/奶奶,我来啦!今天您过得怎么样呀?有没有什么好玩的事儿跟我说说呀?",
"爷爷奶奶,在干嘛呢?",
] ]
} }
]; ];

View File

@ -33,6 +33,9 @@ class WebRTCChat {
this.isVideoReady = false; this.isVideoReady = false;
this.isDefaultVideoLoaded = false; this.isDefaultVideoLoaded = false;
this.retryCount = 0; // 添加重试计数器 this.retryCount = 0; // 添加重试计数器
this.connectionRetryCount = 0; // 连接重试计数器
this.maxRetryAttempts = 5; // 最大重试次数
this.reconnectTimeout = null; // 重连定时器
// 添加视频相关属性 // 添加视频相关属性
this.videoSender = null; // WebRTC视频发送器 this.videoSender = null; // WebRTC视频发送器
@ -172,16 +175,37 @@ class WebRTCChat {
} }
initializeSocket() { initializeSocket() {
this.socket = io(); this.socket = io({
// 客户端配置,与服务器端保持一致
pingTimeout: 60000,
pingInterval: 10000,
upgradeTimeout: 30000,
transports: ['websocket', 'polling']
});
this.socket.on('connect', () => { this.socket.on('connect', () => {
this.updateStatus('已连接到服务器', 'connected'); this.updateStatus('已连接到服务器', 'connected');
this.logMessage('已连接到服务器', 'success'); this.logMessage('已连接到服务器', 'success');
this.connectionRetryCount = 0; // 重置重连计数
}); });
this.socket.on('disconnect', () => { this.socket.on('disconnect', (reason) => {
this.connectionStatus.style.display = 'none'; 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 信令处理 // WebRTC 信令处理
@ -1398,6 +1422,12 @@ class WebRTCChat {
// 清理视频缓存和预创建流 // 清理视频缓存和预创建流
this.clearVideoCache(); this.clearVideoCache();
// 清理重连定时器
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
// setTimeout(() => { // setTimeout(() => {
// // 显示头像,隐藏视频 // // 显示头像,隐藏视频
// if (this.videoContainer) { // if (this.videoContainer) {
@ -1829,6 +1859,36 @@ class WebRTCChat {
// 检查当前视频流状态 // 检查当前视频流状态
this.checkVideoStreamStatus(); 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;
}
} }
// 页面加载完成后初始化应用 // 页面加载完成后初始化应用

View File

@ -70,6 +70,12 @@ async function processAudioQueue() {
// 如果是第一个音频片段,触发视频切换 // 如果是第一个音频片段,触发视频切换
if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.switchVideoStream) { if (sayName != window.webrtcApp.currentVideoTag && window.webrtcApp && window.webrtcApp.switchVideoStream) {
try { try {
// 检查WebSocket连接状态
if (window.webrtcApp.checkConnectionStatus && !window.webrtcApp.checkConnectionStatus()) {
console.log('WebSocket连接异常跳过视频切换');
return;
}
console.log('--------------触发视频切换:', sayName); console.log('--------------触发视频切换:', sayName);
window.webrtcApp.switchVideoStream(targetVideo, 'audio', '8-4-sh'); window.webrtcApp.switchVideoStream(targetVideo, 'audio', '8-4-sh');
isFirstChunk = false; isFirstChunk = false;

Binary file not shown.

Binary file not shown.