private void SimplifyServer_ReceiveStringEvent(HslCommunication.Core.Net.AppSession arg1, NetHandle handle, string msg) { if (handle == 1) { simplifyServer.SendMessage(arg1, handle, version.ToString( )); string address = GetAddressByIp(arg1.IpAddress); lognet.WriteInfo($"{arg1.IpAddress.PadRight( 15 )} [{msg.PadRight( 8 )}] [{address}] Demo"); AddDict(address); } else if (handle == 2) { simplifyServer.SendMessage(arg1, random.Next(10000), "这是一条测试的数据:" + random.Next(10000).ToString( )); } else if (handle == 100) { simplifyServer.SendMessage(arg1, handle, version2.ToString( )); string address = GetAddressByIp(arg1.IpAddress); lognet.WriteInfo($"{arg1.IpAddress.PadRight( 15 )} [{msg.PadRight( 8 )}] [{address}] Controls"); AddDict(address); } else if (handle == 1000) { // 返回统计信息 simplifyServer.SendMessage(arg1, handle, GetData( )); } else { simplifyServer.SendMessage(arg1, handle, msg); } }
/*********************************************************************************************** * * 方法说明: 当接收到来自客户端的数据的时候触发的方法 * 特别注意: 如果你的数据处理中引发了异常,应用程序将会奔溃,SendMessage异常系统将会自动处理 * ************************************************************************************************/ /// <summary> /// 接收到来自客户端的字符串数据,然后将结果发送回客户端,注意:必须回发结果 /// </summary> /// <param name="state">客户端的地址</param> /// <param name="handle">用于自定义的指令头,可不用,转而使用data来区分</param> /// <param name="data">接收到的服务器的数据</param> private void Net_simplify_server_ReceiveStringEvent(HslCommunication.Core.Net.AppSession state, NetHandle handle, string data) { /******************************************************************************************* * * 说明:同步消息处理总站,应该根据不同的消息设置分流到不同的处理方法 * * 注意:处理完成后必须调用 net_simplify_server.SendMessage(state, customer, "处理结果字符串,可以为空"); * *******************************************************************************************/ Console.WriteLine("接受到数据:" + data); if (handle.CodeMajor == 1) { ProcessCodeMajorOne(state, handle, data); } else if (handle.CodeMajor == 2) { ProcessCodeMajorTwo(state, handle, data); } else if (handle.CodeMajor == 3) { ProcessCodeMajorThree(state, handle, data); } else { net_simplify_server.SendMessage(state, handle, data); } }
private void ComplexServer_AcceptByte(HslCommunication.Core.Net.AppSession session, NetHandle handle, byte[] data) { // 接收字节数据, ShowTextInfo($"[{session.IpEndPoint}] [{handle}] {HslCommunication.BasicFramework.SoftBasic.ByteToHexString(data)}"); // 也可以回发客户端信息,选择发送的session即可 }
private void SimplifyServer_ReceiveStringEvent(HslCommunication.Core.Net.AppSession arg1, NetHandle handle, string msg) { if (handle == 1) { simplifyServer.SendMessage(arg1, handle, version.ToString( )); } else { simplifyServer.SendMessage(arg1, handle, msg); } }
/// <summary> /// 接收来自客户端的字节数据 /// </summary> /// <param name="state">网络状态</param> /// <param name="customer">字节数据,根据实际情况选择是否使用</param> /// <param name="data">来自客户端的字节数据</param> private void Net_simplify_server_ReceivedBytesEvent(HslCommunication.Core.Net.AppSession state, NetHandle customer, byte[] data) { if (customer == 1000) { // 收到指令为1000的请求时,返回1000长度的字节数组 net_simplify_server.SendMessage(state, customer, new byte[1000]); } else { net_simplify_server.SendMessage(state, customer, data); } }
private void ProcessCodeMajorThree(HslCommunication.Core.Net.AppSession state, NetHandle handle, string data) { if (handle.CodeIdentifier == 1) { // 下面可以再if..else net_simplify_server.SendMessage(state, handle, "测试数据大类3,命令1,接收到的数据是:" + data); } else { net_simplify_server.SendMessage(state, handle, data); } }
private void ComplexServer_AcceptString(HslCommunication.Core.Net.AppSession session, NetHandle handle, string data) { // 接收字符串 ShowTextInfo($"[{session.IpEndPoint}] [{handle}] {data}"); // 举个例子,当handle==1时,回发一串信息 // for example , when handle == 1. return text if (handle == 1) { complexServer.Send(session, handle, "This is test Text"); } }
private void SimplifyServer_ReceiveStringEvent(HslCommunication.Core.Net.AppSession arg1, NetHandle handle, string msg) { if (handle == 1) { simplifyServer.SendMessage(arg1, handle, version.ToString( )); string address = GetAddressByIp(arg1.IpAddress); lognet.WriteInfo($"{arg1.IpAddress.PadRight( 15 )} [{address}] Run Application"); } else if (handle == 2) { simplifyServer.SendMessage(arg1, random.Next(10000), "这是一条测试的数据:" + random.Next(10000).ToString( )); } else { simplifyServer.SendMessage(arg1, handle, msg); } }
private void ComplexClient_AcceptString(HslCommunication.Core.Net.AppSession session, NetHandle handle, string data) { // 接收字符串 ShowTextInfo($"[{session.IpEndPoint}] [{handle}] {data}"); }
private void ComplexClient_AcceptByte(HslCommunication.Core.Net.AppSession session, NetHandle handle, byte[] data) { // 接收字节数据, ShowTextInfo($"{DateTime.Now} [{session.IpEndPoint}] [{handle}] {HslCommunication.BasicFramework.SoftBasic.ByteToHexString( data )}"); }
private void ComplexServer_ClientOnline(HslCommunication.Core.Net.AppSession session) { // 上线 ShowTextInfo($"[{session.IpEndPoint}] Online"); }
private void ComplexServer_ClientOffline(HslCommunication.Core.Net.AppSession session, string reason) { // 下线 ShowTextInfo($"[{session.IpEndPoint}] Offline"); }
private void ComplexServer_ClientOnline(HslCommunication.Core.Net.AppSession session) { // 上线触发 }
private void ComplexServer_ClientOffline(HslCommunication.Core.Net.AppSession session, string reason) { // 下线触发 }