|
|
using HighWayIot.Config;
|
|
|
using HighWayIot.TouchSocket.Common;
|
|
|
using System;
|
|
|
using System.Text;
|
|
|
using TouchSocket.Core;
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
namespace HighWayIot.TouchSocket
|
|
|
{
|
|
|
public class TouchSocketBusiness
|
|
|
{
|
|
|
private static readonly Lazy<TouchSocketBusiness> lazy = new Lazy<TouchSocketBusiness>(() => new TouchSocketBusiness());
|
|
|
|
|
|
public static TouchSocketBusiness Instance
|
|
|
{
|
|
|
get { return lazy.Value; }
|
|
|
}
|
|
|
#region 委托事件
|
|
|
/// <summary>
|
|
|
/// 刷新扫码器状态
|
|
|
/// </summary>
|
|
|
/// <param name="materialCodeStr"></param>
|
|
|
/// <param name="ip"></param>
|
|
|
public delegate void RefreshState(string ip, bool flag);
|
|
|
public static event RefreshState RefreshStateEvent;
|
|
|
|
|
|
//NoRead事件通知
|
|
|
//public delegate void MessageNoRead();
|
|
|
//public static event MessageNoRead MessageNoReadEvent;
|
|
|
|
|
|
//扫码事件
|
|
|
public delegate void RefreshMaterialCodeStr(string materialCodeStr,string ip);
|
|
|
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
private AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
|
TcpService service;
|
|
|
|
|
|
public TouchSocketBusiness()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
#region Socket 初始化
|
|
|
public MessageModel<string> StartTouchSocket()
|
|
|
{
|
|
|
MessageModel<string> messageModel = new MessageModel<string>();
|
|
|
|
|
|
service = new TcpService();
|
|
|
service.Connecting = (client, e) => {
|
|
|
client.Logger.Info($"{client.IP}:{client.Port} 客户端正在连接...");
|
|
|
return EasyTask.CompletedTask;
|
|
|
};//有客户端正在连接
|
|
|
service.Connected = (client, e) => {
|
|
|
client.Logger.Info($"{client.IP}:{client.Port} 客户端连接成功!目前客户端连接数{service.Count}");
|
|
|
return EasyTask.CompletedTask;
|
|
|
};//有客户端成功连接
|
|
|
service.Disconnected += (client, e) => { //有客户端断开连接
|
|
|
client.Logger.Info($"{client.IP}:{client.Port}客户端断开!");
|
|
|
RefreshStateEvent?.Invoke(client.IP, false);
|
|
|
return EasyTask.CompletedTask;
|
|
|
};
|
|
|
service.Received = (client, byteBlock) =>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var mes = Encoding.UTF8.GetString(byteBlock.ByteBlock.Buffer, 0, byteBlock.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
//心跳包
|
|
|
if (mes == "heartbeat")
|
|
|
{
|
|
|
//扫码器心跳连接
|
|
|
client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
|
|
|
RefreshStateEvent?.Invoke(client.IP, true);
|
|
|
}
|
|
|
else if (mes == "NoRead")
|
|
|
{
|
|
|
client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》NoRead事件{mes}");
|
|
|
// MessageNoReadEvent?.Invoke();
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》接收到信息:{mes}");
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0'),client.IP);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
client.Logger.Error($"异常:{ex.Message}");
|
|
|
}
|
|
|
return EasyTask.CompletedTask;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
service.Setup(new TouchSocketConfig()//载入配置
|
|
|
.SetListenIPHosts(new IPHost[] { new IPHost($"0.0.0.0:5000") })//可同时监听两个地址
|
|
|
// .SetDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); })//配置适配器
|
|
|
.SetMaxCount(10000)
|
|
|
//.SetThreadCount(1000)
|
|
|
//.SetCacheTimeoutEnable(false)
|
|
|
.ConfigureContainer(a =>
|
|
|
{
|
|
|
a.AddConsoleLogger();
|
|
|
})
|
|
|
.ConfigurePlugins(a =>
|
|
|
{
|
|
|
a.UseCheckClear();
|
|
|
}));
|
|
|
service.Start();//启动
|
|
|
|
|
|
service.Logger.Info("扫码服务器成功启动");
|
|
|
messageModel.success = true;
|
|
|
messageModel.msg = "OK";
|
|
|
return messageModel;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
}
|