ChatX/chatclient/MainWindow.xaml.cs

164 lines
5.8 KiB
C#
Raw Normal View History

2025-05-31 20:30:22 +08:00
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;//socket库
using System.Net.Http;
2025-06-01 20:33:52 +08:00
using System.Net;
using System.IO;
using System;
using System.Security.Policy;
using log4net;
using log4net.Config;
2025-06-01 22:34:58 +08:00
using System.Text.Json;
using chatapi;
using System.Diagnostics;
using System.Windows.Interop;
using ControlzEx.Standard;
using System.ComponentModel;
2025-05-31 20:30:22 +08:00
2025-06-01 22:34:58 +08:00
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
2025-05-31 20:30:22 +08:00
namespace chatclient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
2025-05-31 20:30:22 +08:00
{
2025-06-01 22:34:58 +08:00
LoginWindow Login = new();
2025-06-01 20:33:52 +08:00
static string? receive;
public static string? UserName = null;
2025-06-01 20:33:52 +08:00
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
2025-06-01 22:34:58 +08:00
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));
}
2025-05-31 20:30:22 +08:00
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
2025-06-01 20:33:52 +08:00
log.Info("Hello World!");
this.Hide();
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
log.Info($"连接服务器 {Server.ServerIP}:{Server.ServerPort} ");
Client.Connect(Server.ServerIP, Server.ServerPort);
2025-06-01 20:33:52 +08:00
}
2025-06-01 22:34:58 +08:00
catch (Exception ex)
{
Client.Close();
2025-06-01 20:33:52 +08:00
log.Error(ex);
}
Login.Show();
Thread th = new Thread(Receive);
th.Start();
2025-05-31 20:30:22 +08:00
}
2025-06-01 20:33:52 +08:00
static void Receive()
2025-06-01 02:57:57 +08:00
{
2025-06-01 20:33:52 +08:00
byte[] buffer = new byte[1024];
try
{
while (true)
{
2025-06-01 22:34:58 +08:00
int num = Client!.Receive(buffer);
2025-06-01 20:33:52 +08:00
if (num == 0) break;
2025-06-01 22:34:58 +08:00
if (Client.Poll(100, SelectMode.SelectRead) && Client.Available == 0 || !Client.Connected)
{
log.Error("连接已断开");
break;
}
2025-06-01 20:33:52 +08:00
receive = Encoding.UTF8.GetString(buffer, 0, num);
response(receive);
}
}
catch (Exception ex)
{
2025-06-01 20:33:52 +08:00
log.Error(ex);
}
2025-06-01 22:34:58 +08:00
finally
2025-06-01 20:33:52 +08:00
{
2025-06-01 22:34:58 +08:00
Client?.Close();
2025-06-01 20:33:52 +08:00
}
}
2025-06-01 22:34:58 +08:00
static void response(string msg)
{
log.Info($"收到服务器消息: {msg}");
try
{
var Type = JsonSerializer.Deserialize<RegisterData>(msg);
if (Type != null)
{
var LoginResponse = JsonSerializer.Deserialize<LoginResultData>(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<MainWindow>().FirstOrDefault();
if (mainWindow != null)
{
mainWindow.Username = UserName;
mainWindow.Show();
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
loginWindow?.Close();
mainWindow.Activate();
}
});
}
else if (LoginResponse != null)
{
log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}");
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
loginWindow!.LoginMsg = LoginResponse.message;
}
}
else if (Type.type == "login_0")
{
if (LoginResponse != null)
{
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
loginWindow!.LoginMsg = LoginResponse.message;
}
else
{
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
loginWindow!.LoginMsg = "服务器返回错误";
}
}
}
2025-06-01 22:34:58 +08:00
}
catch (JsonException ex)
{
log.Error("JSON解析错误", ex);
}
catch (Exception ex)
{
log.Error("处理响应时发生错误", ex);
2025-06-01 22:34:58 +08:00
}
2025-06-01 02:57:57 +08:00
}
2025-05-31 20:30:22 +08:00
}
}