diff --git a/chatclient/LoginWindow.xaml b/chatclient/LoginWindow.xaml index df76a3c..1fe658d 100644 --- a/chatclient/LoginWindow.xaml +++ b/chatclient/LoginWindow.xaml @@ -1,43 +1,82 @@ - - - - - - - - - - - - - - + + + + + + + + + - - - - + + + + + + + + + + + + diff --git a/chatclient/MainWindow.xaml.cs b/chatclient/MainWindow.xaml.cs index 6ceea4f..ad20a4e 100644 --- a/chatclient/MainWindow.xaml.cs +++ b/chatclient/MainWindow.xaml.cs @@ -15,7 +15,13 @@ using System; using System.Security.Policy; using log4net; using log4net.Config; +using System.Text.Json; +using chatapi; +using System.Diagnostics; +using System.Windows.Interop; +using ControlzEx.Standard; +[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)] namespace chatclient { /// @@ -23,9 +29,12 @@ namespace chatclient /// public partial class MainWindow : Window { + LoginWindow Login = new(); static string? receive; + public static int user_id = 0; + public static string? LoginMsg = "123"; private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow)); - public static Socket Client; + public static Socket? Client; public MainWindow() { InitializeComponent(); @@ -37,10 +46,11 @@ namespace chatclient log.Info("连接服务器 127.0.0.1:5555 "); Client.Connect("127.0.0.1", 5555); } - catch (Exception ex) { + catch (Exception ex) + { + Client.Close(); log.Error(ex); } - LoginWindow Login = new LoginWindow(); Login.Show(); Thread th = new Thread(Receive); th.Start(); @@ -52,8 +62,13 @@ namespace chatclient { while (true) { - int num = Client.Receive(buffer); + int num = Client!.Receive(buffer); if (num == 0) break; + if (Client.Poll(100, SelectMode.SelectRead) && Client.Available == 0 || !Client.Connected) + { + log.Error("连接已断开"); + break; + } receive = Encoding.UTF8.GetString(buffer, 0, num); response(receive); } @@ -61,12 +76,22 @@ namespace chatclient catch (Exception ex) { log.Error(ex); } - finally + finally { - Client.Close(); + Client?.Close(); } } - static void response(string msg) { + static void response(string msg) + { + LoginData data = JsonSerializer.Deserialize(msg)!; + + if (data.success) { + + } + else { + //LoginWindow.Instance.LoginMsg.Text = msg; + + } } } diff --git a/chatclient/chatapi.cs b/chatclient/chatapi.cs index 3f2b168..58f6d4a 100644 --- a/chatclient/chatapi.cs +++ b/chatclient/chatapi.cs @@ -4,9 +4,19 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace chatclient +namespace chatapi { - internal class api + 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; } + } + internal class RegisterData + { + public bool success { get; set; } + public string? message { get; set; } } } diff --git a/chatclient/chatclient.csproj b/chatclient/chatclient.csproj index 5dd3511..c546d44 100644 --- a/chatclient/chatclient.csproj +++ b/chatclient/chatclient.csproj @@ -19,4 +19,10 @@ + + + PreserveNewest + + +