183 lines
5.0 KiB
C#
183 lines
5.0 KiB
C#
|
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;
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Net;
|
|||
|
using System.IO;
|
|||
|
using System;
|
|||
|
using System.Security.Policy;
|
|||
|
using log4net;
|
|||
|
using log4net.Config;
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
>>>>>>> 添加登录ui以及部分交互逻辑
|
|||
|
using System.Text.Json;
|
|||
|
using chatapi;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Windows.Interop;
|
|||
|
using ControlzEx.Standard;
|
|||
|
<<<<<<< HEAD
|
|||
|
=======
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
|
|||
|
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
|
|||
|
=======
|
|||
|
|
|||
|
>>>>>>> 添加项目文件。
|
|||
|
=======
|
|||
|
|
|||
|
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
|
|||
|
>>>>>>> 添加登录ui以及部分交互逻辑
|
|||
|
namespace chatclient
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Interaction logic for MainWindow.xaml
|
|||
|
/// </summary>
|
|||
|
public partial class MainWindow : Window
|
|||
|
{
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
<<<<<<< HEAD
|
|||
|
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 MainWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
log.Info("Hello World!");
|
|||
|
this.Hide();
|
|||
|
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
try
|
|||
|
{
|
|||
|
log.Info("连接服务器 127.0.0.1:5555 ");
|
|||
|
Client.Connect("127.0.0.1", 5555);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Client.Close();
|
|||
|
log.Error(ex);
|
|||
|
}
|
|||
|
Login.Show();
|
|||
|
Thread th = new Thread(Receive);
|
|||
|
th.Start();
|
|||
|
}
|
|||
|
static void Receive()
|
|||
|
{
|
|||
|
byte[] buffer = new byte[1024];
|
|||
|
try
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex) {
|
|||
|
log.Error(ex);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
Client?.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
static void response(string msg)
|
|||
|
{
|
|||
|
|
|||
|
=======
|
|||
|
public MainWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
>>>>>>> 添加项目文件。
|
|||
|
=======
|
|||
|
=======
|
|||
|
LoginWindow Login = new();
|
|||
|
>>>>>>> 添加登录ui以及部分交互逻辑
|
|||
|
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 MainWindow()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
log.Info("Hello World!");
|
|||
|
this.Hide();
|
|||
|
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
try
|
|||
|
{
|
|||
|
log.Info("连接服务器 127.0.0.1:5555 ");
|
|||
|
Client.Connect("127.0.0.1", 5555);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Client.Close();
|
|||
|
log.Error(ex);
|
|||
|
}
|
|||
|
Login.Show();
|
|||
|
Thread th = new Thread(Receive);
|
|||
|
th.Start();
|
|||
|
>>>>>>> 添加基础功能
|
|||
|
}
|
|||
|
static void Receive()
|
|||
|
{
|
|||
|
byte[] buffer = new byte[1024];
|
|||
|
try
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex) {
|
|||
|
log.Error(ex);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
Client?.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
static void response(string msg)
|
|||
|
{
|
|||
|
LoginData data = JsonSerializer.Deserialize<LoginData>(msg)!;
|
|||
|
|
|||
|
if (data.success) {
|
|||
|
|
|||
|
}
|
|||
|
else {
|
|||
|
//LoginWindow.Instance.LoginMsg.Text = msg;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|