Compare commits
3 Commits
20be6f6613
...
34d79871dd
Author | SHA1 | Date | |
---|---|---|---|
34d79871dd | |||
778958042d | |||
253f766732 |
53
chatclient/Data/ChatDataModel.cs
Normal file
53
chatclient/Data/ChatDataModel.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
|
namespace chatclient.Data
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 聊天消息类,表示一条消息的内容和显示属性
|
||||||
|
/// </summary>
|
||||||
|
public class ChatMessage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发送者名称
|
||||||
|
/// </summary>
|
||||||
|
public required string Sender { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 消息类型(文本、图片、文件、系统消息等)
|
||||||
|
/// </summary>
|
||||||
|
public required MessageType Type { get; set; } = MessageType.Text;
|
||||||
|
/// <summary>
|
||||||
|
/// 消息发送者的头像图片
|
||||||
|
/// </summary>
|
||||||
|
public required BitmapImage Image { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 消息内容
|
||||||
|
/// </summary>
|
||||||
|
public required string Content { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 消息发送时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime Timestamp { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 消息在界面中的对齐方式(左/右)
|
||||||
|
/// </summary>
|
||||||
|
public HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
|
||||||
|
/// <summary>
|
||||||
|
/// 发送者名称的显示颜色
|
||||||
|
/// </summary>
|
||||||
|
public Brush SenderColor { get; set; } = Brushes.Black;
|
||||||
|
}
|
||||||
|
public enum MessageType
|
||||||
|
{
|
||||||
|
Text,
|
||||||
|
Image,//图片
|
||||||
|
File,//文件
|
||||||
|
System//系统信息
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
|
using System.Security.RightsManagement;
|
||||||
namespace chatapi
|
|
||||||
|
namespace chatclient.Data
|
||||||
{
|
{
|
||||||
internal class Server
|
internal class Server
|
||||||
{
|
{
|
||||||
@ -33,4 +34,16 @@ namespace chatapi
|
|||||||
{
|
{
|
||||||
public string? type { get; set; }
|
public string? type { get; set; }
|
||||||
}
|
}
|
||||||
|
internal class ChatRegisterData
|
||||||
|
{
|
||||||
|
public string? user { get; set; } = "Unnamed";
|
||||||
|
public string? message { get; set; } = null;
|
||||||
|
public string? image { get; set; } = null;
|
||||||
|
public DateTime timestamp { get; set; } = DateTime.Now;
|
||||||
|
}
|
||||||
|
internal class ChatData
|
||||||
|
{
|
||||||
|
public required string type { get; set; } = "chat";
|
||||||
|
public required string message { get; set; } = "message";
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,9 +6,9 @@ using System.Threading.Tasks;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using log4net;
|
using log4net;
|
||||||
using chatapi;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
|
using chatclient.Data;
|
||||||
|
|
||||||
|
|
||||||
namespace chatclient
|
namespace chatclient
|
||||||
@ -126,7 +126,7 @@ namespace chatclient
|
|||||||
string SignJsonData = JsonSerializer.Serialize(SignData);
|
string SignJsonData = JsonSerializer.Serialize(SignData);
|
||||||
byte[] dataBytes = Encoding.UTF8.GetBytes(SignJsonData);
|
byte[] dataBytes = Encoding.UTF8.GetBytes(SignJsonData);
|
||||||
var content = new StringContent(SignJsonData, Encoding.UTF8, "application/json");
|
var content = new StringContent(SignJsonData, Encoding.UTF8, "application/json");
|
||||||
var response = await MainWindow.HttpClient.PostAsync($"{chatapi.Server.ServerUrl}/api/register", content);
|
var response = await MainWindow.HttpClient.PostAsync($"{Server.ServerUrl}/api/register", content);
|
||||||
var responseBody = await response.Content.ReadAsStringAsync();
|
var responseBody = await response.Content.ReadAsStringAsync();
|
||||||
log.Info($"注册请求已发送,响应内容: {responseBody}");
|
log.Info($"注册请求已发送,响应内容: {responseBody}");
|
||||||
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
var signresponse = JsonSerializer.Deserialize<SignResultData>(responseBody);
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
<Window x:Class="chatclient.MainWindow"
|
<Window
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||||
xmlns:local="clr-namespace:chatclient"
|
xmlns:local="clr-namespace:chatclient"
|
||||||
|
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="chatclient.MainWindow"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380"
|
Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380"
|
||||||
Style="{StaticResource MaterialDesignWindow}">
|
Style="{StaticResource MaterialDesignWindow}">
|
||||||
@ -14,7 +15,7 @@
|
|||||||
<materialDesign:NavigationRailAssist.FloatingContent>
|
<materialDesign:NavigationRailAssist.FloatingContent>
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12" >
|
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12" >
|
||||||
<Image Source="/user.png" />
|
<Image Source="/resource/user.png" />
|
||||||
</Button>
|
</Button>
|
||||||
<TextBlock Text="{Binding Username}"/>
|
<TextBlock Text="{Binding Username}"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
@ -27,7 +28,106 @@
|
|||||||
</StackPanel>
|
</StackPanel>
|
||||||
</TabItem.Header>
|
</TabItem.Header>
|
||||||
<Grid>
|
<Grid>
|
||||||
|
<Grid>
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
<!-- 标题栏 -->
|
||||||
|
<RowDefinition Height="*"/>
|
||||||
|
<!-- 消息区域 -->
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
|
<!-- 输入区域 -->
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
|
<!-- 标题栏 -->
|
||||||
|
<materialDesign:Card Grid.Row="0" Padding="10" Margin="0,0,0,5">
|
||||||
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<TextBlock Text="聊天室" FontSize="16" FontWeight="Bold"
|
||||||
|
VerticalAlignment="Center" Margin="5,0" Cursor="Hand"/>
|
||||||
|
<!--<Button x:Name="btnRefresh" Style="{StaticResource MaterialDesignIconButton}"
|
||||||
|
ToolTip="刷新列表" Click="RefreshContacts_Click" Margin="5,0,10,0">
|
||||||
|
<materialDesign:PackIcon Kind="Refresh"/>
|
||||||
|
</Button>-->
|
||||||
|
<!--<ComboBox x:Name="cmbContacts" Width="150" Margin="5,0"
|
||||||
|
materialDesign:HintAssist.Hint="选择联系人"
|
||||||
|
DisplayMemberPath="DisplayName"/>-->
|
||||||
|
</StackPanel>
|
||||||
|
</materialDesign:Card>
|
||||||
|
|
||||||
|
<!-- 消息区域 -->
|
||||||
|
<ScrollViewer x:Name="messageScroller" Grid.Row="1" VerticalScrollBarVisibility="Auto"
|
||||||
|
Padding="10" Background="{DynamicResource MaterialDesignPaper}">
|
||||||
|
<ItemsControl x:Name="messageList">
|
||||||
|
<ItemsControl.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<Grid HorizontalAlignment="{Binding Alignment}" Margin="0,5">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<!-- 左侧列:头像或空白 -->
|
||||||
|
<ColumnDefinition Width="Auto"/>
|
||||||
|
<!-- 中间列:消息卡片 -->
|
||||||
|
<ColumnDefinition Width="*"/>
|
||||||
|
<!-- 右侧列:头像或空白 -->
|
||||||
|
<ColumnDefinition Width="Auto"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<!-- 左侧头像(仅当Alignment=Left时显示) -->
|
||||||
|
<Border x:Name="leftAvatar" Grid.Column="0" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||||
|
<!-- 图片头像 -->
|
||||||
|
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||||
|
<Image.Clip>
|
||||||
|
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
|
||||||
|
</Image.Clip>
|
||||||
|
</Image>
|
||||||
|
</Border>
|
||||||
|
<!-- 消息卡片(始终在中间列) -->
|
||||||
|
<materialDesign:Card Grid.Column="1" Padding="10" HorizontalAlignment="{Binding Alignment}">
|
||||||
|
<StackPanel>
|
||||||
|
<TextBlock Text="{Binding Sender}" FontWeight="Bold" Foreground="{Binding SenderColor}"/>
|
||||||
|
<TextBlock Text="{Binding Content}" TextWrapping="Wrap" Margin="0,5,0,0"/>
|
||||||
|
<TextBlock Text="{Binding Timestamp, StringFormat='HH:mm:ss'}" Foreground="Gray" FontSize="10" HorizontalAlignment="Right" Margin="0,5,0,0"/>
|
||||||
|
</StackPanel>
|
||||||
|
</materialDesign:Card>
|
||||||
|
<!-- 右侧头像(仅当Alignment=Right时显示) -->
|
||||||
|
<Border x:Name="rightAvatar" Grid.Column="2" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||||
|
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||||
|
<Image.Clip>
|
||||||
|
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
|
||||||
|
</Image.Clip>
|
||||||
|
</Image>
|
||||||
|
</Border>
|
||||||
|
</Grid>
|
||||||
|
<!-- 根据Alignment显示正确的头像 -->
|
||||||
|
<DataTemplate.Triggers>
|
||||||
|
<!-- 左对齐消息:显示左侧头像 -->
|
||||||
|
<DataTrigger Binding="{Binding Alignment}" Value="Left">
|
||||||
|
<Setter TargetName="leftAvatar" Property="Visibility" Value="Visible"/>
|
||||||
|
<Setter TargetName="rightAvatar" Property="Visibility" Value="Collapsed"/>
|
||||||
|
</DataTrigger>
|
||||||
|
<!-- 右对齐消息:显示右侧头像 -->
|
||||||
|
<DataTrigger Binding="{Binding Alignment}" Value="Right">
|
||||||
|
<Setter TargetName="leftAvatar" Property="Visibility" Value="Collapsed"/>
|
||||||
|
<Setter TargetName="rightAvatar" Property="Visibility" Value="Visible"/>
|
||||||
|
</DataTrigger>
|
||||||
|
</DataTemplate.Triggers>
|
||||||
|
</DataTemplate>
|
||||||
|
</ItemsControl.ItemTemplate>
|
||||||
|
</ItemsControl>
|
||||||
|
</ScrollViewer>
|
||||||
|
|
||||||
|
<Grid Grid.Row="2" Background="{DynamicResource MaterialDesign.Brush.Primary.Foreground}">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="*"/>
|
||||||
|
<ColumnDefinition Width="Auto"/>
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
<TextBox x:Name="txtMessage" Grid.Column="0"
|
||||||
|
materialDesign:HintAssist.Hint="输入消息..."
|
||||||
|
AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
|
||||||
|
TextWrapping="Wrap" MinHeight="60" MaxHeight="120" Margin="5"/>
|
||||||
|
|
||||||
|
<Button x:Name="btnSend" Grid.Column="1" Content="发送" MinWidth="80"
|
||||||
|
Style="{StaticResource MaterialDesignRaisedButton}"
|
||||||
|
Click="SendMessage_Click"/>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem>
|
<TabItem>
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
|
||||||
using System.Windows.Documents;
|
using System.Windows.Documents;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
@ -17,11 +16,11 @@ using System.Security.Policy;
|
|||||||
using log4net;
|
using log4net;
|
||||||
using log4net.Config;
|
using log4net.Config;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using chatapi;
|
using chatclient.Data;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Windows.Interop;
|
|
||||||
using ControlzEx.Standard;
|
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows.Threading;
|
||||||
|
using System.Collections.Specialized;
|
||||||
|
|
||||||
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
|
[assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)]
|
||||||
namespace chatclient
|
namespace chatclient
|
||||||
@ -48,6 +47,10 @@ namespace chatclient
|
|||||||
Update("Username");
|
Update("Username");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 消息列表
|
||||||
|
public ObservableCollection<ChatMessage> Messages { get; } = new ObservableCollection<ChatMessage>();
|
||||||
|
private ItemsControl MessageList => messageList;
|
||||||
|
private ScrollViewer MessageScroller => messageScroller;
|
||||||
private void Update(string UpdateName)
|
private void Update(string UpdateName)
|
||||||
{
|
{
|
||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(UpdateName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(UpdateName));
|
||||||
@ -72,8 +75,16 @@ namespace chatclient
|
|||||||
Login.Show();
|
Login.Show();
|
||||||
Thread th = new Thread(Receive);
|
Thread th = new Thread(Receive);
|
||||||
th.Start();
|
th.Start();
|
||||||
|
MessageList.ItemsSource = Messages;
|
||||||
|
((INotifyCollectionChanged)MessageList.Items).CollectionChanged += (s, e) =>
|
||||||
|
{
|
||||||
|
// 确保有足够的时间让UI更新
|
||||||
|
Dispatcher.BeginInvoke(new Action(() =>
|
||||||
|
{
|
||||||
|
MessageScroller.ScrollToEnd();
|
||||||
|
}), DispatcherPriority.ContextIdle);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Receive()
|
static void Receive()
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
@ -101,7 +112,6 @@ namespace chatclient
|
|||||||
Client?.Close();
|
Client?.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void response(string msg)
|
static void response(string msg)
|
||||||
{
|
{
|
||||||
log.Info($"收到服务器消息: {msg}");
|
log.Info($"收到服务器消息: {msg}");
|
||||||
@ -129,26 +139,61 @@ namespace chatclient
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (LoginResponse != null)
|
else
|
||||||
|
{
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||||
|
if (loginWindow != null)
|
||||||
|
{
|
||||||
|
loginWindow.LoginMsg = LoginResponse != null ? LoginResponse.message : "服务器返回错误";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Type.type == "login_0" && LoginResponse != null)
|
||||||
{
|
{
|
||||||
log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}");
|
log.Warn($"登录失败: {LoginResponse.message}\nMsg:{msg}");
|
||||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
loginWindow!.LoginMsg = LoginResponse.message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (Type.type == "login_0")
|
|
||||||
{
|
|
||||||
if (LoginResponse != null)
|
|
||||||
{
|
{
|
||||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
||||||
loginWindow!.LoginMsg = LoginResponse.message;
|
if (loginWindow != null)
|
||||||
|
{
|
||||||
|
loginWindow.LoginMsg = "用户名或密码错误";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (Type.type == "chat")
|
||||||
|
{
|
||||||
|
var chat = JsonSerializer.Deserialize<ChatRegisterData>(msg);
|
||||||
|
if (chat != null)
|
||||||
|
{
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
// 处理聊天消息
|
||||||
|
var chatmessage = new ChatMessage
|
||||||
|
{
|
||||||
|
Sender = chat.user ?? "未知用户",
|
||||||
|
Type = MessageType.Text,
|
||||||
|
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
||||||
|
Content = chat.message ?? "无内容",
|
||||||
|
Timestamp = chat.timestamp,
|
||||||
|
Alignment = HorizontalAlignment.Left,
|
||||||
|
SenderColor = new SolidColorBrush(Colors.Black)
|
||||||
|
};
|
||||||
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||||
|
mainWindow?.Messages.Add(chatmessage);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var loginWindow = Application.Current.Windows.OfType<LoginWindow>().FirstOrDefault();
|
log.Error("反序列化聊天数据时返回了 null");
|
||||||
loginWindow!.LoginMsg = "服务器返回错误";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.Error($"未知的消息类型: {Type.type},请检查服务器响应格式");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (JsonException ex)
|
catch (JsonException ex)
|
||||||
@ -160,5 +205,68 @@ namespace chatclient
|
|||||||
log.Error("处理响应时发生错误", ex);
|
log.Error("处理响应时发生错误", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private void SendMessage_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
SendMessage();
|
||||||
|
}
|
||||||
|
private void SendMessage()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(txtMessage.Text))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// 获取当前选中的联系人
|
||||||
|
//var contact = cmbContacts.SelectedItem as Contact;
|
||||||
|
// 判断是否为群组,若是则收件人设为“所有人”,否则为联系人显示名
|
||||||
|
//string recipient = contact?.IsGroup == true ? "所有人" : contact?.DisplayName;
|
||||||
|
|
||||||
|
// 创建新消息
|
||||||
|
var newMessage = new ChatMessage
|
||||||
|
{
|
||||||
|
Sender = "我",
|
||||||
|
Type = MessageType.Text,
|
||||||
|
Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png", UriKind.Absolute)), // 默认头像
|
||||||
|
Content = txtMessage.Text,
|
||||||
|
Timestamp = DateTime.Now,
|
||||||
|
Alignment = HorizontalAlignment.Right, // 自己发送的消息靠右
|
||||||
|
SenderColor = new SolidColorBrush(Colors.Blue)
|
||||||
|
};
|
||||||
|
var newChatMessage = new ChatData
|
||||||
|
{
|
||||||
|
type = "chat",
|
||||||
|
message = newMessage.Content
|
||||||
|
};
|
||||||
|
string ChatJsonData = JsonSerializer.Serialize(newChatMessage);
|
||||||
|
byte[] dataBytes = Encoding.UTF8.GetBytes(ChatJsonData);
|
||||||
|
log.Info($"向服务器聊天信息(长度:{dataBytes.Length})");
|
||||||
|
if (Client != null)
|
||||||
|
{
|
||||||
|
if (Client.Connected)
|
||||||
|
{
|
||||||
|
Client.Send(dataBytes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
log.Info("未连接服务器,尝试连接");
|
||||||
|
Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
Client.Connect(IPAddress.Parse(Server.ServerIP), Server.ServerPort);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.Error(ex);
|
||||||
|
Client.Close();
|
||||||
|
}
|
||||||
|
//finally
|
||||||
|
//{
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 添加到消息列表
|
||||||
|
Messages.Add(newMessage);
|
||||||
|
// 清空输入框
|
||||||
|
txtMessage.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,9 +6,23 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
|
<ApplicationIcon>resource\chat.ico</ApplicationIcon>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<DebugType>full</DebugType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Content Include="resource\chat.ico" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
|
||||||
<PackageReference Include="log4net" Version="3.1.0" />
|
<PackageReference Include="log4net" Version="3.1.0" />
|
||||||
<PackageReference Include="MaterialDesignThemes" Version="5.2.1" />
|
<PackageReference Include="MaterialDesignThemes" Version="5.2.1" />
|
||||||
<PackageReference Include="MaterialDesignThemes.MahApps" Version="5.2.1" />
|
<PackageReference Include="MaterialDesignThemes.MahApps" Version="5.2.1" />
|
||||||
@ -16,7 +30,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Resource Include="user.png" />
|
<Resource Include="resource\user.png" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
BIN
chatclient/resource/chat.ico
Normal file
BIN
chatclient/resource/chat.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
Loading…
x
Reference in New Issue
Block a user