diff --git a/ShengShengBuXi/Hubs/AudioHub.cs b/ShengShengBuXi/Hubs/AudioHub.cs index 70eea15..67f0f5c 100644 --- a/ShengShengBuXi/Hubs/AudioHub.cs +++ b/ShengShengBuXi/Hubs/AudioHub.cs @@ -81,6 +81,10 @@ namespace ShengShengBuXi.Hubs /// private static readonly string _sentencesFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config/sentences.txt"); /// + /// 真实用户显示记录的持久化文件路径 + /// + private static readonly string _realUserDisplayLogsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config/real_user_displays.log"); + /// /// 预设句子列表 /// private static List _presetSentences = new List(); @@ -493,7 +497,7 @@ namespace ShengShengBuXi.Hubs try { byte[] dataToSend; - + if (_configurationService.CurrentConfig.Network.EnableAudioNoiseReduction) { dataToSend = _audioProcessingService.ApplyNoiseReduction(audioData, config.SampleRate, config.Channels); @@ -504,7 +508,7 @@ namespace ShengShengBuXi.Hubs dataToSend = audioData; _logger.LogDebug($"转发音频数据到{monitoringClients.Count}个监听客户端,数据长度: {audioData.Length}"); } - + // 始终使用二进制格式发送数据,避免字符串转换 await Clients.Clients(monitoringClients).SendAsync("ReceiveAudioData", dataToSend); } @@ -924,7 +928,7 @@ namespace ShengShengBuXi.Hubs { // 兼容旧版客户端的方法 _logger.LogWarning($"客户端 {Context.ConnectionId} 使用了已废弃的StartReceivingDisplayText方法,应改为GetNextDisplayText"); - + // 转发到新方法 await GetNextDisplayText(); } @@ -989,6 +993,12 @@ namespace ShengShengBuXi.Hubs // 只发送给请求的客户端 await Clients.Caller.SendAsync("ReceiveDisplayText", textToDisplay.Text); _logger.LogInformation($"已发送显示文本到客户端: {textToDisplay.Text} (来源: {(textToDisplay.IsRealUser ? "真实用户" : "预设文本")})"); + + // 如果是真实用户的发言,持久化保存到文件 + if (textToDisplay.IsRealUser) + { + SaveRealUserDisplayToFile(textToDisplay); + } } else { @@ -1060,7 +1070,7 @@ namespace ShengShengBuXi.Hubs // 写入默认的预设句子 File.WriteAllLines(_sentencesFilePath, new string[] { - + }); } @@ -1564,5 +1574,38 @@ namespace ShengShengBuXi.Hubs await Clients.Client(clientId).SendAsync("ReceiveDisplayConfig", configJson); } } + + /// + /// 保存真实用户显示文本到文件 + /// + /// 要保存的显示文本对象 + private void SaveRealUserDisplayToFile(DisplayText displayText) + { + try + { + // 确保目录存在 + string directory = Path.GetDirectoryName(_realUserDisplayLogsPath); + if (!Directory.Exists(directory)) + { + Directory.CreateDirectory(directory); + } + + // 使用简单的序列化方式,将对象转换为单行文本 + string entry = JsonConvert.SerializeObject(new + { + displayText.Text, + Timestamp = displayText.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"), + }); + + // 使用AppendAllText确保追加到文件末尾,不覆盖原有内容 + File.AppendAllText(_realUserDisplayLogsPath, entry + Environment.NewLine); + + _logger.LogInformation($"已保存真实用户显示文本到文件: {displayText.Text}"); + } + catch (Exception ex) + { + _logger.LogError($"保存真实用户显示文本失败: {ex.Message}"); + } + } } } \ No newline at end of file