From e01af0bcc23fa67792de765a9d77e31201c792ca Mon Sep 17 00:00:00 2001 From: XuShanQiXun <3401460572@qq.com> Date: Sun, 22 Jun 2025 01:41:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=92=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `MainWindow.xaml.cs` 中,调整了消息对齐逻辑,新增 `SenderColor` 属性以根据用户 ID 设置消息颜色。发送者为当前用户时,消息右对齐并显示蓝色;否则左对齐并显示黑色。 在 `Program.cs` 中,移除了对 HTTP 请求的处理逻辑,简化了客户端连接处理。同时,更新了日志记录,确保准确反映压缩数据的实际长度。 --- chatclient/MainWindow.xaml.cs | 9 ++++++--- chatserver/Program.cs | 29 ++++++++++++++--------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/chatclient/MainWindow.xaml.cs b/chatclient/MainWindow.xaml.cs index 63ca0b5..8f1e903 100644 --- a/chatclient/MainWindow.xaml.cs +++ b/chatclient/MainWindow.xaml.cs @@ -386,8 +386,11 @@ namespace chatclient Content = msgItem.message ?? "(无内容)", Timestamp = msgItem.timestamp ?? DateTime.Now, Alignment = msgItem.userid == UserId ? - HorizontalAlignment.Right : - HorizontalAlignment.Left + HorizontalAlignment.Right : + HorizontalAlignment.Left, + SenderColor = msgItem.userid == UserId ? + new SolidColorBrush(Colors.Blue) : + new SolidColorBrush(Colors.Black) }; mainWindow.Messages.Insert(0, chatmessage); }); @@ -470,7 +473,7 @@ namespace chatclient Buffer.BlockCopy(lengthPrefix, 0, fullMessage, 0, lengthPrefix.Length); Buffer.BlockCopy(compressedData, 0, fullMessage, lengthPrefix.Length, compressedData.Length); - log.Info($"发送数据(长度:{data.Length},压缩后长度:{lengthPrefix.Length},总体长度:{fullMessage.Length})"); + log.Info($"发送数据(长度:{data.Length},压缩后长度:{compressedData.Length},总体长度:{fullMessage.Length})"); Client?.Send(fullMessage); } private async void SendMessage_Click(object sender, RoutedEventArgs e) diff --git a/chatserver/Program.cs b/chatserver/Program.cs index f83f425..cdc2b46 100644 --- a/chatserver/Program.cs +++ b/chatserver/Program.cs @@ -58,24 +58,23 @@ namespace chatserver } static void HandleClient(Socket socket) { - const int prefixSize = sizeof(int); - byte[] prefixBuffer = new byte[prefixSize]; - int received = socket.Receive(prefixBuffer, 0, 4, SocketFlags.Peek); - if (received == 4) - { - string httpStart = Encoding.ASCII.GetString(prefixBuffer); - if (httpStart.StartsWith("GET") || httpStart.StartsWith("POST") || - httpStart.StartsWith("HEAD") || httpStart.StartsWith("HTTP")) - { - log.Warn("检测到HTTP请求,拒绝连接"); - socket.Close(); - return; - } - } try { while (true) { + const int prefixSize = sizeof(int); + byte[] prefixBuffer = new byte[prefixSize]; + int received = socket.Receive(prefixBuffer, 0, 4, SocketFlags.Peek); + if (received == 4) + { + string httpStart = Encoding.ASCII.GetString(prefixBuffer); + if (httpStart.StartsWith("GET") || httpStart.StartsWith("POST") || + httpStart.StartsWith("HEAD") || httpStart.StartsWith("HTTP")) + { + log.Warn("检测到HTTP请求,拒绝连接"); + return; + } + } //读取长度前缀 int prefixBytesRead = 0; while (prefixBytesRead < prefixSize) @@ -453,7 +452,7 @@ namespace chatserver Buffer.BlockCopy(lengthPrefix, 0, fullMessage, 0, lengthPrefix.Length); Buffer.BlockCopy(compressedData, 0, fullMessage, lengthPrefix.Length, compressedData.Length); - log.Info($"发送数据(长度:{data.Length},压缩后长度:{lengthPrefix.Length},总体长度:{fullMessage.Length})"); + log.Info($"发送数据(长度:{data.Length},压缩后长度:{compressedData.Length},总体长度:{fullMessage.Length})"); socket.Send(fullMessage); } ///