From 5d2fe7190a301c849410ba57af8875b8502ed4f4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Thu, 2 Mar 2023 12:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehttpserver=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Example/Scenes/ServerExample.unity | 2 +- .../HttpServer/HttpServerModel.cs | 46 +++++++-- .../HttpServer/HttpServerView.cs | 93 +++++-------------- 3 files changed, 62 insertions(+), 79 deletions(-) diff --git a/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity b/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity index c9ebfc5..127c462 100644 --- a/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity +++ b/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity @@ -667,7 +667,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: - m_Target: {fileID: 1020834818} - m_MethodName: startListen + m_MethodName: startServer m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs index c60db12..5fbe77d 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs @@ -6,6 +6,7 @@ using System.Net; using System.Threading; using QFrameworkCP; using System.Text; +using System.IO; namespace JXSoft { public class HttpServerModel : DataEventModel @@ -40,7 +41,7 @@ namespace JXSoft { public Thread reciveT; public bool startServer(string[] prefixes) { - if (!httpServer.IsListening) { + if (httpServer == null || !httpServer.IsListening) { if (!HttpListener.IsSupported) { Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); @@ -60,7 +61,9 @@ namespace JXSoft { httpServer.Prefixes.Add(s); } httpServer.Start(); - Debug.Log("Listening..."); + reciveT = new Thread(RecciveMsg); + reciveT.IsBackground = true; + reciveT.Start(); return true; } return false; @@ -79,30 +82,55 @@ namespace JXSoft { public void closeServer() { if (httpServer.IsListening) { - httpServer.Stop(); + reciveT.Abort(); + httpServer.Close(); } } public void RecciveMsg() { - byte[] receiveBuff = new byte[1024]; - int reviceLength = 0; string msg = ""; while (httpServer.IsListening) { try { HttpListenerContext context = httpServer.GetContext(); + #region 解析收到的消息 HttpListenerRequest request = context.Request; - // Obtain a response object. - //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(request.Url); - //reviceLength = tcpClient.Client.Receive(receiveBuff); - msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength); + switch (request.HttpMethod) + { + case "POST": + { + Stream stream = context.Request.InputStream; + StreamReader reader = new StreamReader(stream, Encoding.UTF8); + msg = reader.ReadToEnd(); + } + break; + case "GET": + { + var data = request.QueryString; + msg = data.ToString(); + } + break; + } if (msg != "") { receivedData = msg; isReceivedValue = true; } + #endregion + + #region 返回消息 + HttpListenerResponse response = context.Response; + // Construct a response. + byte[] buffer = Encoding.UTF8.GetBytes(" " + "success" + ""); + // Get a response stream and write the response to it. + response.ContentLength64 = buffer.Length; + System.IO.Stream output = response.OutputStream; + output.Write(buffer, 0, buffer.Length); + // You must close the output stream. + output.Close(); + #endregion } catch (Exception e) { diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs index 8a4f58c..1a2668a 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs @@ -6,82 +6,37 @@ using System.Net; using System.Text; using UnityEngine; -public class HttpServerView : MonoBehaviour -{ - public string[] prefixes; - // Start is called before the first frame update - void Start() +namespace JXSoft { + public class HttpServerView : MonoBehaviour { - - } - - // Update is called once per frame - void Update() - { - - } - - public void startListen() { - SimpleListenerExample(prefixes); - } - - // This example requires the System and System.Net namespaces. - public static void SimpleListenerExample(string[] prefixes) - { - if (!HttpListener.IsSupported) + public string[] prefixes; + public HttpServerUtility httpServer = new HttpServerUtility(); + // Start is called before the first frame update + void Start() { - Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); - return; + //httpServer.startServer(prefixes); } - // URI prefixes are required, - // for example "http://127.0.0.1:8080/index/". - if (prefixes == null || prefixes.Length == 0) - throw new ArgumentException("prefixes"); - // Create a listener. - HttpListener listener = new HttpListener(); - // Add the prefixes. - foreach (string s in prefixes) + // Update is called once per frame + void Update() { - listener.Prefixes.Add(s); + if (httpServer != null && !"".Equals(httpServer.getReceivedValue())) + { + /*GameObject item = Instantiate(tcpMsgItem, tcpMsgContent); + item.GetComponentInChildren().text = tcpUtil.receivedData; + this.GetModel().onDataRecived.Invoke(tcpUtil.receivedData); + */ + Debug.Log(httpServer.receivedData); + } } - listener.Start(); - Debug.Log("Listening..."); - // Note: The GetContext method blocks while waiting for a request. - HttpListenerContext context = listener.GetContext(); - - #region 解析Request请求 - HttpListenerRequest request = context.Request; - string content = ""; - switch (request.HttpMethod) + public void startServer() { + bool isSuccess = httpServer.startServer(prefixes); + Debug.Log("ServerOpen:"+isSuccess); + } + private void OnDestroy() { - case "POST": - { - Stream stream = context.Request.InputStream; - StreamReader reader = new StreamReader(stream, Encoding.UTF8); - content = reader.ReadToEnd(); - Debug.Log(content); - } - break; - case "GET": - { - var data = request.QueryString; - Debug.Log(data); - } - break; + httpServer.closeServer(); } - - #endregion - // Obtain a response object. - HttpListenerResponse response = context.Response; - // Construct a response. - byte[] buffer = Encoding.UTF8.GetBytes(" " + request.InputStream + ""); - // Get a response stream and write the response to it. - response.ContentLength64 = buffer.Length; - System.IO.Stream output = response.OutputStream; - output.Write(buffer, 0, buffer.Length); - // You must close the output stream. - output.Close(); - listener.Stop(); } } +