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 lazy = new Lazy(() => new TouchSocketBusiness()); public static TouchSocketBusiness Instance { get { return lazy.Value; } } #region 委托事件 /// /// 刷新扫码器状态 /// /// /// 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); public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; //出库扫码 public delegate void OutMaterialCodeStr(string materialCodeStr); public static event OutMaterialCodeStr OutMaterialCodeStrEvent; #endregion private AppConfig appConfig = AppConfig.Instance; TcpService service; public TouchSocketBusiness() { } #region Socket 初始化 public MessageModel StartTouchSocket() { MessageModel messageModel = new MessageModel(); 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") { if (client.IP == appConfig.foamHikRobotIp) { client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》NoRead事件{mes}"); MessageNoReadEvent?.Invoke(); } } else { // ReceiveCodeDelegateEvent?.Invoke(client.IP, mes); // 入库扫码器 if (client.IP == appConfig.foamHikRobotIp) { client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》接收到信息:{mes}"); RefreshMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0')); } else if (client.IP == appConfig.foamOutHikRobotIp) { client.Logger.Info($"出库扫码器{client.IP}:{client.Port}》接收到信息:{mes}"); OutMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0')); } } } 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 } }