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/LoginWindow.xaml.cs b/chatclient/LoginWindow.xaml.cs
index 75c56ae..2e3f9ee 100644
--- a/chatclient/LoginWindow.xaml.cs
+++ b/chatclient/LoginWindow.xaml.cs
@@ -17,6 +17,9 @@ using System.Net.Sockets;
using System.IO;
using log4net;
using log4net.Config;
+using System.Windows.Interop;
+using chatapi;
+using System.Runtime.CompilerServices;
namespace chatclient
@@ -24,7 +27,7 @@ namespace chatclient
///
/// LoginWindow.xaml 的交互逻辑
///
- public partial class LoginWindow : Window,INotifyPropertyChanged
+ public partial class LoginWindow : Window, INotifyPropertyChanged
{
private static readonly ILog log = LogManager.GetLogger(typeof(LoginWindow));
public LoginWindow()
@@ -32,37 +35,45 @@ namespace chatclient
InitializeComponent();
this.DataContext = this;
}
-
public event PropertyChangedEventHandler? PropertyChanged;
- private void Update (string UpdateName)
+ private void Update(string UpdateName)
{
- PropertyChangedEventHandler? handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(UpdateName));
- }
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(UpdateName));
}
- private string? _LoginMsg;
- public string UserName { get; set; } = "";
- public string UserPassword { get; set; } = "";
- public string? LoginMsg
+ public static string UserName { get; set; } = "";
+ public static string UserPassword { get; set; } = "";
+ public static string SignName { get; set; } = "";
+ public static string SignPassword1 { get; set; } = "";
+ public static string SignPassword2 { get; set; } = "";
+ private string? _SignMsg;
+ public string? SignMsg
{
- get { return _LoginMsg; }
+ get { return _SignMsg; }
set
{
- _LoginMsg = value;
- Update("LoginMsg");
+ _SignMsg = value;
+ Update("SignMsg");
+ }
+ }
+ private bool _SaveAccount;
+ public bool SaveAccount
+ {
+ get { return _SaveAccount; }
+ set
+ {
+ _SaveAccount = value;
+ Update("SaveAccount");
}
}
private void Login_Click(object sender, RoutedEventArgs e)
{
if (UserName == "")
{
- LoginMsg = "用户名不能为空";
+ LoginMsg.Text = "用户名不能为空";
}
else if (UserPassword == "")
{
- LoginMsg = "密码不能为空";
+ LoginMsg.Text = "密码不能为空";
}
else
{
@@ -72,9 +83,91 @@ namespace chatclient
username = UserName,
password = UserPassword
};
- string LoginLsonData = JsonSerializer.Serialize(LoginData);
- byte[] dataBytes = Encoding.UTF8.GetBytes(LoginLsonData);
- MainWindow.Client.Send(dataBytes);
+ string LoginJsonData = JsonSerializer.Serialize(LoginData);
+ byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
+ log.Info($"向服务器发送登录请求(UserName:{UserName})");
+ if (MainWindow.Client is not null)
+ {
+ if (MainWindow.Client.Connected)
+ {
+ MainWindow.Client.Send(dataBytes);
+ }
+ else
+ {
+ try
+ {
+ log.Info("未连接服务器,尝试连接");
+ MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ MainWindow.Client.Connect("127.0.0.1", 5555);
+ }
+ catch (Exception ex)
+ {
+ log.Error(ex);
+ MainWindow.Client.Close();
+ }
+ finally
+ {
+ LoginMsg.Text = "服务器连接失败或已断开连接";
+ }
+ }
+ }
+ }
+ }
+ private void Sign_Click(object sender, RoutedEventArgs e)
+ {
+ if (SignName == "")
+ {
+ SignMsg = "用户名不能为空";
+ }
+ else if (SignPassword1 == "")
+ {
+ SignMsg = "密码不能为空";
+ }
+ else if (SignPassword2 == "")
+ {
+ SignMsg = "请再次确认密码";
+ }
+ else if (SignPassword1 != SignPassword2)
+ {
+ SignMsg = "两次密码输入不一致";
+ }
+ else
+ {
+ var SignData = new
+ {
+ type = "register",
+ username = SignName,
+ password = SignPassword1
+ };
+ string SignJsonData = JsonSerializer.Serialize(SignData);
+ byte[] dataBytes = Encoding.UTF8.GetBytes(SignJsonData);
+ log.Info($"向服务器发送注册请求(UserName:{SignName})");
+ Update("LoginMsg");
+ if (MainWindow.Client is not null)
+ {
+ if (MainWindow.Client.Connected)
+ {
+ MainWindow.Client.Send(dataBytes);
+ }
+ else
+ {
+ try
+ {
+ log.Info("未连接服务器,尝试连接");
+ MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ MainWindow.Client.Connect("127.0.0.1", 5555);
+ }
+ catch (Exception ex)
+ {
+ log.Error(ex);
+ MainWindow.Client.Close();
+ }
+ finally
+ {
+ SignMsg = "服务器连接失败或已断开连接";
+ }
+ }
+ }
}
}
}
diff --git a/chatclient/MainWindow.xaml b/chatclient/MainWindow.xaml
index 2633770..7e0af05 100644
--- a/chatclient/MainWindow.xaml
+++ b/chatclient/MainWindow.xaml
@@ -12,8 +12,7 @@
-
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
+
+
+