diff --git a/chatclient/LoginWindow.xaml b/chatclient/LoginWindow.xaml index 1fe658d..13b9691 100644 --- a/chatclient/LoginWindow.xaml +++ b/chatclient/LoginWindow.xaml @@ -11,7 +11,7 @@ ResizeMode="NoResize"> - + @@ -33,7 +33,7 @@ Style="{StaticResource MaterialDesignOutlinedRevealPasswordBox}"/> - + + + + + diff --git a/chatclient/MainWindow.xaml.cs b/chatclient/MainWindow.xaml.cs index ad20a4e..6d64d54 100644 --- a/chatclient/MainWindow.xaml.cs +++ b/chatclient/MainWindow.xaml.cs @@ -8,7 +8,8 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; -using System.Net.Sockets; +using System.Net.Sockets;//socket库 +using System.Net.Http; using System.Net; using System.IO; using System; @@ -20,6 +21,7 @@ using chatapi; using System.Diagnostics; using System.Windows.Interop; using ControlzEx.Standard; +using System.ComponentModel; [assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)] namespace chatclient @@ -27,24 +29,40 @@ namespace chatclient /// /// Interaction logic for MainWindow.xaml /// - public partial class MainWindow : Window + public partial class MainWindow : Window, INotifyPropertyChanged { LoginWindow Login = new(); static string? receive; - public static int user_id = 0; - public static string? LoginMsg = "123"; + public static string? UserName = null; private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow)); public static Socket? Client; + public static readonly HttpClient HttpClient = new HttpClient(); + public event PropertyChangedEventHandler? PropertyChanged; + private string? _Username; + public string? Username + { + get { return _Username; } + set + { + _Username = value; + Update("Username"); + } + } + private void Update(string UpdateName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(UpdateName)); + } public MainWindow() { InitializeComponent(); + this.DataContext = this; log.Info("Hello World!"); this.Hide(); Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { - log.Info("连接服务器 127.0.0.1:5555 "); - Client.Connect("127.0.0.1", 5555); + log.Info($"连接服务器 {Server.ServerIP}:{Server.ServerPort} "); + Client.Connect(Server.ServerIP, Server.ServerPort); } catch (Exception ex) { @@ -55,6 +73,7 @@ namespace chatclient Thread th = new Thread(Receive); th.Start(); } + static void Receive() { byte[] buffer = new byte[1024]; @@ -73,7 +92,8 @@ namespace chatclient response(receive); } } - catch (Exception ex) { + catch (Exception ex) + { log.Error(ex); } finally @@ -81,18 +101,64 @@ namespace chatclient Client?.Close(); } } - static void response(string msg) + + static void response(string msg) { - LoginData data = JsonSerializer.Deserialize(msg)!; - - if (data.success) { - + log.Info($"收到服务器消息: {msg}"); + try + { + var Type = JsonSerializer.Deserialize(msg); + if (Type != null) + { + var LoginResponse = JsonSerializer.Deserialize(msg); + if (Type.type == "login_1") + { + if (LoginResponse!.status == "success" && LoginResponse != null) + { + log.Info($"用户 {UserName} 登录成功"); + Application.Current.Dispatcher.Invoke(() => + { + var mainWindow = Application.Current.Windows.OfType().FirstOrDefault(); + if (mainWindow != null) + { + mainWindow.Username = UserName; + mainWindow.Show(); + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + loginWindow?.Close(); + mainWindow.Activate(); + } + }); + } + else if (LoginResponse != null) + { + log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}"); + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + loginWindow!.LoginMsg = LoginResponse.message; + } + } + else if (Type.type == "login_0") + { + if (LoginResponse != null) + { + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + loginWindow!.LoginMsg = LoginResponse.message; + } + else + { + var loginWindow = Application.Current.Windows.OfType().FirstOrDefault(); + loginWindow!.LoginMsg = "服务器返回错误"; + } + } + } } - else { - //LoginWindow.Instance.LoginMsg.Text = msg; - + catch (JsonException ex) + { + log.Error("JSON解析错误", ex); + } + catch (Exception ex) + { + log.Error("处理响应时发生错误", ex); } - } } } \ No newline at end of file diff --git a/chatclient/chatapi.cs b/chatclient/chatapi.cs index 58f6d4a..c5c3e56 100644 --- a/chatclient/chatapi.cs +++ b/chatclient/chatapi.cs @@ -1,22 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - + namespace chatapi { + internal class Server + { + public const string ServerUrl = "http://127.0.0.1:5001"; + public const string ServerIP = "127.0.0.1"; + public const int ServerPort = 8889; + } internal class LoginData { - public bool success { get; set; } - public string? message { get; set; } - public string? token { get; set; } - public int? user_id { get; set; } - public string? username { get; set; } + public string? type { get; set; } = null; + public string? username { get; set; } = null; + public string? password { get; set; } = null; + } + internal class LoginResultData + { + public string? status { get; set; } = null; + public string? message { get; set; } = null; + } + internal class SignData + { + public string? type { get; set; } = null; + public string? username { get; set; } = null; + public string? password { get; set; } = null; + } + internal class SignResultData + { + public bool success { get; set; } = false; + public string? message { get; set; } = null; } internal class RegisterData { - public bool success { get; set; } - public string? message { get; set; } + public string? type { get; set; } } }