Compare commits
No commits in common. "c3496de067dadf4564601ced99f6e394bd50f5bc" and "dc076bff9fb3284ebcdcb9a789edfcbbd1205a44" have entirely different histories.
c3496de067
...
dc076bff9f
@ -13,13 +13,11 @@ namespace chatclient.Data
|
|||||||
public string? type { get; set; } = null;
|
public string? type { get; set; } = null;
|
||||||
public string? username { get; set; } = null;
|
public string? username { get; set; } = null;
|
||||||
public string? password { get; set; } = null;
|
public string? password { get; set; } = null;
|
||||||
public string? token { get; set; } = null;
|
|
||||||
}
|
}
|
||||||
internal class LoginResultData
|
internal class LoginResultData
|
||||||
{
|
{
|
||||||
public string? status { get; set; }
|
public string? status { get; set; } = null;
|
||||||
public string? message { get; set; }
|
public string? message { get; set; } = null;
|
||||||
public string? token { get; set; }
|
|
||||||
}
|
}
|
||||||
internal class SignData
|
internal class SignData
|
||||||
{
|
{
|
||||||
@ -39,7 +37,6 @@ namespace chatclient.Data
|
|||||||
internal class ChatRegisterData
|
internal class ChatRegisterData
|
||||||
{
|
{
|
||||||
public string? user { get; set; } = "Unnamed";
|
public string? user { get; set; } = "Unnamed";
|
||||||
public string? status { get; set; } = null;
|
|
||||||
public string? message { get; set; } = null;
|
public string? message { get; set; } = null;
|
||||||
public string? image { get; set; } = null;
|
public string? image { get; set; } = null;
|
||||||
public DateTime timestamp { get; set; } = DateTime.Now;
|
public DateTime timestamp { get; set; } = DateTime.Now;
|
||||||
|
@ -9,8 +9,6 @@ using log4net;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using chatclient.Data;
|
using chatclient.Data;
|
||||||
using System.Net;
|
|
||||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
|
|
||||||
|
|
||||||
|
|
||||||
namespace chatclient
|
namespace chatclient
|
||||||
@ -66,7 +64,7 @@ namespace chatclient
|
|||||||
Update("SaveAccount");
|
Update("SaveAccount");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private async void Login_Click(object sender, RoutedEventArgs e)
|
private void Login_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (UserName == "")
|
if (UserName == "")
|
||||||
{
|
{
|
||||||
@ -78,7 +76,7 @@ namespace chatclient
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await Login(false, UserName, UserPassword);
|
Login(false,UserName,UserPassword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void Sign_Click(object sender, RoutedEventArgs e)
|
private void Sign_Click(object sender, RoutedEventArgs e)
|
||||||
@ -134,8 +132,18 @@ namespace chatclient
|
|||||||
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
||||||
if (signresponse!.success)
|
if (signresponse!.success)
|
||||||
{
|
{
|
||||||
|
// 显示 MainWindow
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
var mainWindow = Application.Current.Windows
|
||||||
|
.OfType<MainWindow>()
|
||||||
|
.FirstOrDefault();
|
||||||
|
});
|
||||||
log.Info($"注册成功: {signresponse.message}");
|
log.Info($"注册成功: {signresponse.message}");
|
||||||
await Login(true, Username, Userpassword);
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
Login(true, Username, Userpassword);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -156,9 +164,12 @@ namespace chatclient
|
|||||||
log.Info("注册请求已完成");
|
log.Info("注册请求已完成");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static async Task Login(bool Sign, string Username, string Userpassword)
|
//var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||||
|
//loginWindow?.Close();
|
||||||
|
public static void Login(bool Sign,string Username, string Userpassword)
|
||||||
|
{
|
||||||
|
if (Sign)
|
||||||
{
|
{
|
||||||
// 公共的登录数据准备
|
|
||||||
var LoginData = new
|
var LoginData = new
|
||||||
{
|
{
|
||||||
type = "login",
|
type = "login",
|
||||||
@ -167,43 +178,82 @@ namespace chatclient
|
|||||||
};
|
};
|
||||||
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
||||||
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
|
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
|
||||||
log.Info($"向服务器发送登录请求(UserName:{Username})");
|
log.Info($"向服务器发送登录请求(UserName:{UserName})");
|
||||||
// 检查Socket是否可用
|
if (MainWindow.Client is not null)
|
||||||
if (MainWindow.Client?.Connected == true)
|
{
|
||||||
|
if (MainWindow.Client.Connected)
|
||||||
{
|
{
|
||||||
MainWindow.Client.Send(dataBytes);
|
MainWindow.Client.Send(dataBytes);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
log.Info("未连接服务器,尝试异步连接");
|
else
|
||||||
// 异步连接操作
|
|
||||||
await Task.Run(() =>
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
log.Info("未连接服务器,尝试连接");
|
||||||
MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
MainWindow.Client?.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
MainWindow.Client.Connect(System.Net.IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||||
MainWindow.StartReceive();
|
|
||||||
MainWindow.Client?.Send(dataBytes);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
log.Error($"连接失败: {ex.Message}");
|
log.Error(ex);
|
||||||
MainWindow.Client?.Close();
|
MainWindow.Client.Close();
|
||||||
// 根据Sign类型更新UI
|
}
|
||||||
string errorMsg = Sign ?
|
finally
|
||||||
"在完成注册后与服务器断开连接\n请尝试在登录窗口重新登录" :
|
{
|
||||||
"服务器连接失败";
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||||
|
loginWindow!.SignMsg = "在完成注册后与服务器断开连接\n请尝试在登录窗口重新登录";
|
||||||
|
});
|
||||||
|
//"服务器连接失败";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var LoginData = new
|
||||||
|
{
|
||||||
|
type = "login",
|
||||||
|
username = Username,
|
||||||
|
password = Userpassword
|
||||||
|
};
|
||||||
|
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(System.Net.IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.Error(ex);
|
||||||
|
MainWindow.Client.Close();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||||
if (loginWindow != null)
|
if (loginWindow != null)
|
||||||
{
|
{
|
||||||
if (Sign) loginWindow.SignMsg = errorMsg;
|
loginWindow.LoginMsg = "服务器连接失败";
|
||||||
else loginWindow.LoginMsg = errorMsg;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void Window_Loaded(object sender, RoutedEventArgs e)
|
public void Window_Loaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
@ -32,8 +32,7 @@ namespace chatclient
|
|||||||
{
|
{
|
||||||
LoginWindow Login = new();
|
LoginWindow Login = new();
|
||||||
static string? receive;
|
static string? receive;
|
||||||
public static string UserName { get; set; } = "?";
|
public static string? UserName = null;
|
||||||
public static string? token = null;
|
|
||||||
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
|
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
|
||||||
public static Socket? Client;
|
public static Socket? Client;
|
||||||
public static readonly HttpClient HttpClient = new HttpClient();
|
public static readonly HttpClient HttpClient = new HttpClient();
|
||||||
@ -74,7 +73,8 @@ namespace chatclient
|
|||||||
log.Error(ex);
|
log.Error(ex);
|
||||||
}
|
}
|
||||||
Login.Show();
|
Login.Show();
|
||||||
if(Client != null && Client.Connected == true) StartReceive();
|
Thread th = new Thread(Receive);
|
||||||
|
th.Start();
|
||||||
MessageList.ItemsSource = Messages;
|
MessageList.ItemsSource = Messages;
|
||||||
((INotifyCollectionChanged)MessageList.Items).CollectionChanged += (s, e) =>
|
((INotifyCollectionChanged)MessageList.Items).CollectionChanged += (s, e) =>
|
||||||
{
|
{
|
||||||
@ -85,15 +85,6 @@ namespace chatclient
|
|||||||
}), DispatcherPriority.ContextIdle);
|
}), DispatcherPriority.ContextIdle);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
public static void StartReceive()
|
|
||||||
{
|
|
||||||
if (Client != null && Client.Connected == true)
|
|
||||||
{
|
|
||||||
Thread th = new Thread(Receive);
|
|
||||||
th.Start();
|
|
||||||
}
|
|
||||||
else { log.Fatal("在Client为NULL或未连接服务器时被调用StartReceive()"); }
|
|
||||||
}
|
|
||||||
static void Receive()
|
static void Receive()
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
@ -134,7 +125,7 @@ namespace chatclient
|
|||||||
{
|
{
|
||||||
if (LoginResponse!.status == "success" && LoginResponse != null)
|
if (LoginResponse!.status == "success" && LoginResponse != null)
|
||||||
{
|
{
|
||||||
token = LoginResponse.token;
|
log.Info($"用户 {UserName} 登录成功");
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||||
@ -147,7 +138,6 @@ namespace chatclient
|
|||||||
mainWindow.Activate();
|
mainWindow.Activate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
log.Info($"用户 {UserName} 登录成功(token:{token})");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -203,11 +193,6 @@ namespace chatclient
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
log.Error($"未知的消息类型: {Type.type},请检查服务器响应格式");
|
log.Error($"未知的消息类型: {Type.type},请检查服务器响应格式");
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
|
||||||
{
|
|
||||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
|
||||||
if (loginWindow != null) loginWindow.LoginMsg = "服务器返回了错误的值";
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,22 +33,10 @@
|
|||||||
<Resource Include="resource\user.png" />
|
<Resource Include="resource\user.png" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Update="Data\DataSet.Designer.cs">
|
|
||||||
<DesignTime>True</DesignTime>
|
|
||||||
<AutoGen>True</AutoGen>
|
|
||||||
<DependentUpon>DataSet.xsd</DependentUpon>
|
|
||||||
</Compile>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="config\log4net.config">
|
<None Update="config\log4net.config">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
<None Update="Data\DataSet.xsd">
|
|
||||||
<Generator>MSDataSetGenerator</Generator>
|
|
||||||
<LastGenOutput>DataSet.Designer.cs</LastGenOutput>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -2,15 +2,15 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<log4net>
|
<log4net>
|
||||||
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
|
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
|
||||||
<datePattern value="yyyyMMdd'_chat.log'" />
|
<datePattern value="yyyyMM\\yyyyMMdd'_chat.log'" />
|
||||||
<encoding value="utf-8" />
|
<encoding value="utf-8" />
|
||||||
<file value="log\\" />
|
<file value="..\\..\\log\\" />
|
||||||
<appendToFile value="true" />
|
<appendToFile value="true" />
|
||||||
<rollingStyle value="Date" />
|
<rollingStyle value="Date" />
|
||||||
<staticLogFileName value="false" />
|
<staticLogFileName value="false" />
|
||||||
<param name="MaxSizeRollBackups" value="100" />
|
<param name="MaxSizeRollBackups" value="100" />
|
||||||
<layout type="log4net.Layout.PatternLayout">
|
<layout type="log4net.Layout.PatternLayout">
|
||||||
<conversionPattern value="[%date %thread %logger %-5level]%ndc - %message%newline" />
|
<conversionPattern value="[%date %thread %-5level %logger]%ndc - %message%newline" />
|
||||||
</layout>
|
</layout>
|
||||||
</appender>
|
</appender>
|
||||||
<root>
|
<root>
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 87 KiB |
Binary file not shown.
Before Width: | Height: | Size: 128 KiB |
Loading…
x
Reference in New Issue
Block a user