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 _logger; public TcpMonitor( ILogger 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(); } } }