优化消息显示和客户端连接处理
在 `MainWindow.xaml.cs` 中,调整了消息对齐逻辑,新增 `SenderColor` 属性以根据用户 ID 设置消息颜色。发送者为当前用户时,消息右对齐并显示蓝色;否则左对齐并显示黑色。 在 `Program.cs` 中,移除了对 HTTP 请求的处理逻辑,简化了客户端连接处理。同时,更新了日志记录,确保准确反映压缩数据的实际长度。
This commit is contained in:
parent
015535c100
commit
e01af0bcc2
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user