321
This commit is contained in:
parent
b7ab90865c
commit
e67668188e
|
|
@ -81,6 +81,10 @@ namespace ShengShengBuXi.Hubs
|
|||
/// </summary>
|
||||
private static readonly string _sentencesFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config/sentences.txt");
|
||||
/// <summary>
|
||||
/// 真实用户显示记录的持久化文件路径
|
||||
/// </summary>
|
||||
private static readonly string _realUserDisplayLogsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config/real_user_displays.log");
|
||||
/// <summary>
|
||||
/// 预设句子列表
|
||||
/// </summary>
|
||||
private static List<string> _presetSentences = new List<string>();
|
||||
|
|
@ -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
|
||||
{
|
||||
|
|
@ -1564,5 +1574,38 @@ namespace ShengShengBuXi.Hubs
|
|||
await Clients.Client(clientId).SendAsync("ReceiveDisplayConfig", configJson);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存真实用户显示文本到文件
|
||||
/// </summary>
|
||||
/// <param name="displayText">要保存的显示文本对象</param>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user