diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 0260d28..7d24db9 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -75,6 +75,8 @@ namespace formula_manage InitializeComponent(); this.Closing += Window_Closing; //添加窗口关闭事件 + UserClass.HttpSERVER.Start(); //开启网路通讯 + USER.Text = App.USER_Purview; RRODUCTdataTable.Columns.Add("ID", typeof(int)); RRODUCTdataTable.Columns.Add("DYELOT", typeof(string)); @@ -263,6 +265,8 @@ namespace formula_manage System.IO.DirectoryInfo log = new System.IO.DirectoryInfo(@logpath);//生成日志文件目录 FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write); StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine(Log_time + "HttpSERVER_STOP"); + UserClass.HttpSERVER.Stop(); //停止网络通信 wr.WriteLine(Log_time + "FORMULA_STOP"); wr.Close(); } diff --git a/UserClass/HttpSERVER.cs b/UserClass/HttpSERVER.cs new file mode 100644 index 0000000..5032f9a --- /dev/null +++ b/UserClass/HttpSERVER.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.IO; +using System.Net; + + +namespace formula_manage.UserClass +{ + public class HttpSERVER + { + private static bool isExcute = true; + private static HttpListener listener = new HttpListener(); + public static void Start() + { + System.Threading.ThreadPool.QueueUserWorkItem(w => Excute());//单独开启一个线程执行监听消息 + } + private static void Excute() + { + if (HttpListener.IsSupported) + { + if (!listener.IsListening) + { + listener.Prefixes.Add("http://127.0.0.1:8888/"); //添加需要监听的url + listener.Start(); //开始监听端口,接收客户端请求 + } + while (isExcute) + { + try + { + //阻塞主函数至接收到一个客户端请求为止 等待请求 + HttpListenerContext context = listener.GetContext(); + //解析请求 + HttpListenerRequest request = context.Request; + //构造响应 + HttpListenerResponse response = context.Response; + //http请求方式:get,post等等 + string httpMethod = request.HttpMethod?.ToLower(); + string rawUrl = request.RawUrl;//不包括IP和端口 + var Url = request.Url;//全路径 + + if (httpMethod == "get") + { + //获取查询参数 + var queryString = request.QueryString; + // 请求接口 test/method?id=5 + //键值对方式 string val = queryString["key"]; + //string val = queryString["id"];val的值是5 + } + else if (httpMethod == "post") + { + //请求接口 test/postMethod 格式是json + //{ + // "id":5, + // "name":"zs" + //} + //获取pots请求的请求体,json格式的字符串 + var reader = new StreamReader(request.InputStream); + var questBody = reader.ReadToEnd(); + if (!string.IsNullOrEmpty(rawUrl)) + { + //这里可以直接用switch这个只是demo + if (rawUrl.Equals("/server/uploadgenconnected", StringComparison.OrdinalIgnoreCase)) + { + if (!string.IsNullOrEmpty(questBody)) + { + UploadGenConnectedModel model = JsonConvert.DeserializeObject(questBody); + if (model != null) + { + // To Do + } + } + } + } + } + + var responseString = string.Empty; + // 执行其他业务逻辑 + //***************** + responseString = JsonConvert.SerializeObject(new { code = 1, msg = "发送成功" }); + byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); + //对客户端输出相应信息. + response.ContentLength64 = buffer.Length; + //response.StatusCode = 200; + //response.ContentType = "text/plain"; + //发送响应 + using (System.IO.Stream output = response.OutputStream) + { + output.Write(buffer, 0, buffer.Length); + } + } + catch (Exception exceotion) + { + //Logger.Error("处理消息异常", exceotion); + string str = exceotion.Message; + } + } + } + else + { + //Logger.Info("系统不支持HttpListener"); + } + } + + public static void Stop() + { + isExcute = false; + if (listener.IsListening) + listener.Stop(); + } + } + + public class UploadGenConnectedModel + { + public bool GenConnected { get; set; } + } +} diff --git a/formula_manage.csproj b/formula_manage.csproj index 9b2898e..5636fbd 100644 --- a/formula_manage.csproj +++ b/formula_manage.csproj @@ -164,6 +164,9 @@ packages\SamOatesGames.ModernUI.Xceed.Toolkit.1.0.1905191936\lib\net45\ModernUI.Xceed.Toolkit.dll + + packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll + packages\NModbus.3.0.81\lib\net46\NModbus.dll @@ -263,6 +266,7 @@ + diff --git a/packages.config b/packages.config index d468ea7..0fd8730 100644 --- a/packages.config +++ b/packages.config @@ -11,6 +11,7 @@ +