From eb24450089bc2ad3e5327b7f6f6ac23000502229 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Thu, 2 Mar 2023 11:34:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86httpserver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...erverExample.unity => ServerExample.unity} | 17 ++- ...le.unity.meta => ServerExample.unity.meta} | 0 .../HttpServer/HttpServerModel.cs | 135 ++++++++++++++++++ .../HttpServer/HttpServerModel.cs.meta | 11 ++ .../HttpServer/HttpServerView.cs | 87 +++++++++++ .../HttpServer/HttpServerView.cs.meta | 11 ++ .../TCPServer/Script/SocketServer.cs | 1 + 7 files changed, 260 insertions(+), 2 deletions(-) rename Assets/MsgTransmitTools/Example/Scenes/{TCPServerExample.unity => ServerExample.unity} (97%) rename Assets/MsgTransmitTools/Example/Scenes/{TCPServerExample.unity.meta => ServerExample.unity.meta} (100%) create mode 100644 Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs create mode 100644 Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta create mode 100644 Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs create mode 100644 Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta diff --git a/Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity b/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity similarity index 97% rename from Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity rename to Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity index 5842a51..c9ebfc5 100644 --- a/Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity +++ b/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity @@ -400,9 +400,11 @@ MonoBehaviour: m_GameObject: {fileID: 1020834817} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: d21a45ed4d4194444984809e9676e7fb, type: 3} + m_Script: {fileID: 11500000, guid: df3c647a96e64d14f9aedcd84c2b3433, type: 3} m_Name: m_EditorClassIdentifier: + prefixes: + - http://192.168.1.41:8443/index/ --- !u!4 &1020834819 Transform: m_ObjectHideFlags: 0 @@ -663,7 +665,18 @@ MonoBehaviour: m_TargetGraphic: {fileID: 2100516202} m_OnClick: m_PersistentCalls: - m_Calls: [] + m_Calls: + - m_Target: {fileID: 1020834818} + m_MethodName: startListen + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!114 &2100516202 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity.meta b/Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity.meta similarity index 100% rename from Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity.meta rename to Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity.meta diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs new file mode 100644 index 0000000..c60db12 --- /dev/null +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs @@ -0,0 +1,135 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; +using System.Net; +using System.Threading; +using QFrameworkCP; +using System.Text; + +namespace JXSoft { + public class HttpServerModel : DataEventModel + { + + protected override void OnInit() + { + + } + + public override void setLinkState(int linkState) + { + + } + + public override int getLinkState() + { + return 0; + } + } + + public class HttpServerUtility:IUtility + { + //example:http://127.0.0.1:8080/index + public string address; + public HttpListener httpServer; + + public bool isReceivedValue = false; + public string receivedData = ""; + public string exceptionData = ""; + + public Thread reciveT; + + public bool startServer(string[] prefixes) { + if (!httpServer.IsListening) { + if (!HttpListener.IsSupported) + { + Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); + return false; + } + // 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. + httpServer = new HttpListener(); + // Add the prefixes. + foreach (string s in prefixes) + { + httpServer.Prefixes.Add(s); + } + httpServer.Start(); + Debug.Log("Listening..."); + return true; + } + return false; + } + + public void startServer(string prefix) { + string[] prefixes = {prefix}; + startServer(prefixes); + } + + public void startServer() + { + string[] prefixes = { address }; + startServer(prefixes); + } + + public void closeServer() { + if (httpServer.IsListening) { + httpServer.Stop(); + } + } + + public void RecciveMsg() + { + byte[] receiveBuff = new byte[1024]; + int reviceLength = 0; + string msg = ""; + while (httpServer.IsListening) + { + try + { + HttpListenerContext context = httpServer.GetContext(); + 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); + if (msg != "") + { + receivedData = msg; + isReceivedValue = true; + } + } + catch (Exception e) + { + Debug.LogWarning(e); + //断线发送异常 + exceptionData = e.ToString(); + break; + } + } + Debug.Log("------------end While-------------"); + } + + /// + /// 线程接收到消息后 + /// + /// 接收到的消息 + public string getReceivedValue() + { + if (isReceivedValue) + { + isReceivedValue = false; + return receivedData; + } + else + { + return ""; + } + } + } +} \ No newline at end of file diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta new file mode 100644 index 0000000..610eb98 --- /dev/null +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb431867bcae5b949a6115a944655b4e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs new file mode 100644 index 0000000..8a4f58c --- /dev/null +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +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() + { + + } + + // 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) + { + Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); + return; + } + // 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) + { + listener.Prefixes.Add(s); + } + 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) + { + 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; + } + + #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(); + } +} diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta new file mode 100644 index 0000000..ac1c0d6 --- /dev/null +++ b/Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: df3c647a96e64d14f9aedcd84c2b3433 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs b/Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs index a4072df..be050b6 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs @@ -15,6 +15,7 @@ public class SocketServer : MonoBehaviour string _ip = "192.168.1.41"; Thread thread; + void Start() { StartServer();