|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using TouchSocket;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
using static SocketExample.TCPWindowV2;
|
|
|
|
|
namespace SocketExample
|
|
|
|
|
{
|
|
|
|
|
public class MyTouchClass
|
|
|
|
|
{
|
|
|
|
|
public sealed class MySessionClient : TcpSessionClient
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected override async Task OnTcpReceived(ReceivedDataEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//此处逻辑单线程处理。
|
|
|
|
|
|
|
|
|
|
//此处处理数据,功能相当于Received委托。
|
|
|
|
|
var mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
|
|
|
|
MessageBox.Show($"已接收到信息:{mes}");
|
|
|
|
|
|
|
|
|
|
await base.OnTcpReceived(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MyService : TcpService<MySessionClient>
|
|
|
|
|
{
|
|
|
|
|
protected override void LoadConfig(TouchSocketConfig config)
|
|
|
|
|
{
|
|
|
|
|
//此处加载配置,用户可以从配置中获取配置项。
|
|
|
|
|
base.LoadConfig(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override MySessionClient NewClient()
|
|
|
|
|
{
|
|
|
|
|
return new MySessionClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnTcpConnecting(MySessionClient socketClient, ConnectingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//此处逻辑会多线程处理。
|
|
|
|
|
|
|
|
|
|
//e.Id:对新连接的客户端进行ID初始化,默认情况下是按照设定的规则随机分配的。
|
|
|
|
|
//但是按照需求,您可以自定义设置,例如设置为其IP地址。但是需要注意的是id必须在生命周期内唯一。
|
|
|
|
|
|
|
|
|
|
//e.IsPermitOperation:指示是否允许该客户端链接。
|
|
|
|
|
|
|
|
|
|
await base.OnTcpConnecting(socketClient, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|