添加分类

This commit is contained in:
绪山七寻 2025-06-06 22:50:21 +08:00
parent 253f766732
commit 778958042d
3 changed files with 38 additions and 12 deletions

View File

@ -32,4 +32,11 @@
{
public string? type { get; set; }
}
internal class ChatData
{
public string? user { get; set; } = "Unnamed";
public string? message { get; set; } = null;
public string? image { get; set; } = null;
public DateTime timestamp { get; set; } = DateTime.Now;
}
}

View File

@ -112,7 +112,6 @@ namespace chatclient
Client?.Close();
}
}
static void response(string msg)
{
log.Info($"收到服务器消息: {msg}");
@ -152,28 +151,48 @@ namespace chatclient
});
}
}
else if(Type.type == "login_0" && LoginResponse != null)
{
else if (Type.type == "login_0" && LoginResponse != null)
{
log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}");
Application.Current.Dispatcher.Invoke(() =>
{
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
if (loginWindow != null)
{
loginWindow.LoginMsg = LoginResponse.message;
loginWindow.LoginMsg = "用户名或密码错误";
}
});
}
else if (Type.type == "chat")
{
var chat = JsonSerializer.Deserialize<ChatData>(msg);
if (chat != null)
{
// 处理聊天消息
var chatmessage = new ChatMessage
{
Sender = chat.user ?? "未知用户",
Type = MessageType.Text,
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/user.png", UriKind.Absolute)),
Content = chat.message ?? "无内容",
Timestamp = chat.timestamp,
Alignment = HorizontalAlignment.Left,
SenderColor = new SolidColorBrush(Colors.Black)
};
Application.Current.Dispatcher.Invoke(() =>
{
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
mainWindow?.Messages.Add(chatmessage);
});
}
else
{
log.Error("反序列化聊天数据时返回了 null");
}
}
else
{
Application.Current.Dispatcher.Invoke(() =>
{
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
if (loginWindow != null)
{
loginWindow.LoginMsg = LoginResponse != null ? LoginResponse.message : "服务器返回错误";
}
});
log.Error($"未知的消息类型: {Type.type},请检查服务器响应格式");
}
}
}

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB