TouchSocketTest/TcpMonitor.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2025-03-27 13:55:14 +08:00
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace TouchSocketTest
{
public partial class TcpMonitor : PluginBase, ITcpReceivedPlugin, ITcpConnectingPlugin,ITcpConnectedPlugin,ITcpClosedPlugin
{
private readonly ILogger<TcpMonitor> _logger;
public TcpMonitor(
ILogger<TcpMonitor> logger)
{
_logger = logger;
}
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
_logger.LogInformation($"收到{client.IP}{client.GetIPPort()}的消息{e.ByteBlock.Span.ToString(Encoding.UTF8)}");
await e.InvokeNext();
}
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
{
_logger.LogInformation($"{client.IP}{client.GetIPPort()}正在连接!");
await e.InvokeNext();
}
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
{
_logger.LogInformation($"{client.IP}{client.GetIPPort()}已连接!");
await e.InvokeNext();
}
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
{
_logger.LogInformation($"{client.IP}{client.GetIPPort()}已关闭!");
await e.InvokeNext();
}
}
}