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? username { get; set; } = null;
|
||||
public string? password { get; set; } = null;
|
||||
public string? token { get; set; } = null;
|
||||
}
|
||||
internal class LoginResultData
|
||||
{
|
||||
public string? status { get; set; }
|
||||
public string? message { get; set; }
|
||||
public string? token { get; set; }
|
||||
public string? status { get; set; } = null;
|
||||
public string? message { get; set; } = null;
|
||||
}
|
||||
internal class SignData
|
||||
{
|
||||
@ -39,7 +37,6 @@ namespace chatclient.Data
|
||||
internal class ChatRegisterData
|
||||
{
|
||||
public string? user { get; set; } = "Unnamed";
|
||||
public string? status { get; set; } = null;
|
||||
public string? message { get; set; } = null;
|
||||
public string? image { get; set; } = null;
|
||||
public DateTime timestamp { get; set; } = DateTime.Now;
|
||||
|
@ -9,8 +9,6 @@ using log4net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.Win32;
|
||||
using chatclient.Data;
|
||||
using System.Net;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
|
||||
|
||||
|
||||
namespace chatclient
|
||||
@ -66,7 +64,7 @@ namespace chatclient
|
||||
Update("SaveAccount");
|
||||
}
|
||||
}
|
||||
private async void Login_Click(object sender, RoutedEventArgs e)
|
||||
private void Login_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (UserName == "")
|
||||
{
|
||||
@ -78,7 +76,7 @@ namespace chatclient
|
||||
}
|
||||
else
|
||||
{
|
||||
await Login(false, UserName, UserPassword);
|
||||
Login(false,UserName,UserPassword);
|
||||
}
|
||||
}
|
||||
private void Sign_Click(object sender, RoutedEventArgs e)
|
||||
@ -134,8 +132,18 @@ namespace chatclient
|
||||
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
||||
if (signresponse!.success)
|
||||
{
|
||||
// 显示 MainWindow
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var mainWindow = Application.Current.Windows
|
||||
.OfType<MainWindow>()
|
||||
.FirstOrDefault();
|
||||
});
|
||||
log.Info($"注册成功: {signresponse.message}");
|
||||
await Login(true, Username, Userpassword);
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Login(true, Username, Userpassword);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -156,54 +164,96 @@ namespace chatclient
|
||||
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)
|
||||
{
|
||||
// 公共的登录数据准备
|
||||
var LoginData = new
|
||||
if (Sign)
|
||||
{
|
||||
type = "login",
|
||||
username = Username,
|
||||
password = Userpassword
|
||||
};
|
||||
string LoginJsonData = JsonSerializer.Serialize(LoginData);
|
||||
byte[] dataBytes = Encoding.UTF8.GetBytes(LoginJsonData);
|
||||
log.Info($"向服务器发送登录请求(UserName:{Username})");
|
||||
// 检查Socket是否可用
|
||||
if (MainWindow.Client?.Connected == true)
|
||||
{
|
||||
MainWindow.Client.Send(dataBytes);
|
||||
return;
|
||||
}
|
||||
log.Info("未连接服务器,尝试异步连接");
|
||||
// 异步连接操作
|
||||
await Task.Run(() =>
|
||||
{
|
||||
try
|
||||
var LoginData = new
|
||||
{
|
||||
MainWindow.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
MainWindow.Client?.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||
MainWindow.StartReceive();
|
||||
MainWindow.Client?.Send(dataBytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
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)
|
||||
{
|
||||
log.Error($"连接失败: {ex.Message}");
|
||||
MainWindow.Client?.Close();
|
||||
// 根据Sign类型更新UI
|
||||
string errorMsg = Sign ?
|
||||
"在完成注册后与服务器断开连接\n请尝试在登录窗口重新登录" :
|
||||
"服务器连接失败";
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
if (MainWindow.Client.Connected)
|
||||
{
|
||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||
if (loginWindow != null)
|
||||
MainWindow.Client.Send(dataBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Sign) loginWindow.SignMsg = errorMsg;
|
||||
else loginWindow.LoginMsg = errorMsg;
|
||||
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(() =>
|
||||
{
|
||||
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(() =>
|
||||
{
|
||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||
if (loginWindow != null)
|
||||
{
|
||||
loginWindow.LoginMsg = "服务器连接失败";
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
@ -32,8 +32,7 @@ namespace chatclient
|
||||
{
|
||||
LoginWindow Login = new();
|
||||
static string? receive;
|
||||
public static string UserName { get; set; } = "?";
|
||||
public static string? token = null;
|
||||
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();
|
||||
@ -74,7 +73,8 @@ namespace chatclient
|
||||
log.Error(ex);
|
||||
}
|
||||
Login.Show();
|
||||
if(Client != null && Client.Connected == true) StartReceive();
|
||||
Thread th = new Thread(Receive);
|
||||
th.Start();
|
||||
MessageList.ItemsSource = Messages;
|
||||
((INotifyCollectionChanged)MessageList.Items).CollectionChanged += (s, e) =>
|
||||
{
|
||||
@ -85,15 +85,6 @@ namespace chatclient
|
||||
}), 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()
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
@ -134,7 +125,7 @@ namespace chatclient
|
||||
{
|
||||
if (LoginResponse!.status == "success" && LoginResponse != null)
|
||||
{
|
||||
token = LoginResponse.token;
|
||||
log.Info($"用户 {UserName} 登录成功");
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||
@ -147,7 +138,6 @@ namespace chatclient
|
||||
mainWindow.Activate();
|
||||
}
|
||||
});
|
||||
log.Info($"用户 {UserName} 登录成功(token:{token})");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -203,11 +193,6 @@ namespace chatclient
|
||||
else
|
||||
{
|
||||
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" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Data\DataSet.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>DataSet.xsd</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="config\log4net.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Data\DataSet.xsd">
|
||||
<Generator>MSDataSetGenerator</Generator>
|
||||
<LastGenOutput>DataSet.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -2,15 +2,15 @@
|
||||
<configuration>
|
||||
<log4net>
|
||||
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
|
||||
<datePattern value="yyyyMMdd'_chat.log'" />
|
||||
<datePattern value="yyyyMM\\yyyyMMdd'_chat.log'" />
|
||||
<encoding value="utf-8" />
|
||||
<file value="log\\" />
|
||||
<file value="..\\..\\log\\" />
|
||||
<appendToFile value="true" />
|
||||
<rollingStyle value="Date" />
|
||||
<staticLogFileName value="false" />
|
||||
<param name="MaxSizeRollBackups" value="100" />
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="[%date %thread %logger %-5level]%ndc - %message%newline" />
|
||||
<conversionPattern value="[%date %thread %-5level %logger]%ndc - %message%newline" />
|
||||
</layout>
|
||||
</appender>
|
||||
<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