|
|
@ -1,5 +1,5 @@
|
|
|
|
using Admin.Core.Common;
|
|
|
|
using Admin.Core.Common;
|
|
|
|
using Admin.Core.Socket.TSocket;
|
|
|
|
|
|
|
|
using log4net;
|
|
|
|
using log4net;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading;
|
|
|
@ -50,17 +50,22 @@ namespace Admin.Core.Socket
|
|
|
|
MessageModel<string> messageModel = new MessageModel<string>();
|
|
|
|
MessageModel<string> messageModel = new MessageModel<string>();
|
|
|
|
|
|
|
|
|
|
|
|
service = new TcpService();
|
|
|
|
service = new TcpService();
|
|
|
|
service.Connecting = (client, e) => { client.Logger.Info($"{client.IP}:{client.Port} 客户端正在连接..."); };//有客户端正在连接
|
|
|
|
service.Connecting = (client, e) => { client.Logger.Info($"{client.IP}:{client.Port} 客户端正在连接...");
|
|
|
|
service.Connected = (client, e) => { client.Logger.Info($"{client.IP}:{client.Port} 客户端连接成功!目前客户端连接数{service.Count}"); };//有客户端成功连接
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
|
|
|
};//有客户端正在连接
|
|
|
|
|
|
|
|
service.Connected = (client, e) => { client.Logger.Info($"{client.IP}:{client.Port} 客户端连接成功!目前客户端连接数{service.Count}");
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
|
|
|
};//有客户端成功连接
|
|
|
|
service.Disconnected += (client, e) => { //有客户端断开连接
|
|
|
|
service.Disconnected += (client, e) => { //有客户端断开连接
|
|
|
|
client.Logger.Info($"{client.IP}:{client.Port}客户端断开!");
|
|
|
|
client.Logger.Info($"{client.IP}:{client.Port}客户端断开!");
|
|
|
|
RefreshStateEvent?.Invoke(client.IP, false);
|
|
|
|
RefreshStateEvent?.Invoke(client.IP, false);
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
service.Received = (client, byteBlock, requestInfo) =>
|
|
|
|
service.Received = (client, byteBlock) =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
|
var mes = Encoding.UTF8.GetString(byteBlock.ByteBlock.Buffer, 0, byteBlock.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
|
//心跳包
|
|
|
|
//心跳包
|
|
|
|
if (mes== "heartbeat")
|
|
|
|
if (mes== "heartbeat")
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -86,12 +91,24 @@ namespace Admin.Core.Socket
|
|
|
|
{
|
|
|
|
{
|
|
|
|
client.Logger.Error($"异常:{ex.Message}");
|
|
|
|
client.Logger.Error($"异常:{ex.Message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// service.Setup(new TouchSocketConfig()//载入配置
|
|
|
|
|
|
|
|
//.SetListenIPHosts(new IPHost[] { new IPHost($"0.0.0.0:5000") })
|
|
|
|
|
|
|
|
//.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// a.AddConsoleLogger();
|
|
|
|
|
|
|
|
//})
|
|
|
|
|
|
|
|
//.ConfigurePlugins(a =>
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// //自定义插件
|
|
|
|
|
|
|
|
//}));
|
|
|
|
|
|
|
|
|
|
|
|
service.Setup(new TouchSocketConfig()//载入配置
|
|
|
|
service.Setup(new TouchSocketConfig()//载入配置
|
|
|
|
.SetListenIPHosts(new IPHost[] { new IPHost(Appsettings.app("Middleware", "TouchSocket", "Address").ObjToString()) })//可同时监听两个地址
|
|
|
|
.SetListenIPHosts(new IPHost[] { new IPHost($"0.0.0.0:5000") })//可同时监听两个地址
|
|
|
|
.SetDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); })//配置适配器
|
|
|
|
// .SetDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); })//配置适配器
|
|
|
|
.SetMaxCount(10000)
|
|
|
|
.SetMaxCount(10000)
|
|
|
|
//.SetThreadCount(1000)
|
|
|
|
//.SetThreadCount(1000)
|
|
|
|
//.SetCacheTimeoutEnable(false)
|
|
|
|
//.SetCacheTimeoutEnable(false)
|
|
|
@ -102,8 +119,8 @@ namespace Admin.Core.Socket
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
a.UseCheckClear();
|
|
|
|
a.UseCheckClear();
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
.Start();//启动
|
|
|
|
service.Start();//启动
|
|
|
|
|
|
|
|
|
|
|
|
service.Logger.Info("服务器成功启动");
|
|
|
|
service.Logger.Info("服务器成功启动");
|
|
|
|
messageModel.success = true;
|
|
|
|
messageModel.success = true;
|
|
|
|