Browse Source

新增httpserver通用类

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
5d2fe7190a
  1. 2
      Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity
  2. 46
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs
  3. 93
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs

2
Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity

@ -667,7 +667,7 @@ MonoBehaviour:
m_PersistentCalls: m_PersistentCalls:
m_Calls: m_Calls:
- m_Target: {fileID: 1020834818} - m_Target: {fileID: 1020834818}
m_MethodName: startListen m_MethodName: startServer
m_Mode: 1 m_Mode: 1
m_Arguments: m_Arguments:
m_ObjectArgument: {fileID: 0} m_ObjectArgument: {fileID: 0}

46
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs

@ -6,6 +6,7 @@ using System.Net;
using System.Threading; using System.Threading;
using QFrameworkCP; using QFrameworkCP;
using System.Text; using System.Text;
using System.IO;
namespace JXSoft { namespace JXSoft {
public class HttpServerModel : DataEventModel public class HttpServerModel : DataEventModel
@ -40,7 +41,7 @@ namespace JXSoft {
public Thread reciveT; public Thread reciveT;
public bool startServer(string[] prefixes) { public bool startServer(string[] prefixes) {
if (!httpServer.IsListening) { if (httpServer == null || !httpServer.IsListening) {
if (!HttpListener.IsSupported) if (!HttpListener.IsSupported)
{ {
Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); 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.Prefixes.Add(s);
} }
httpServer.Start(); httpServer.Start();
Debug.Log("Listening..."); reciveT = new Thread(RecciveMsg);
reciveT.IsBackground = true;
reciveT.Start();
return true; return true;
} }
return false; return false;
@ -79,30 +82,55 @@ namespace JXSoft {
public void closeServer() { public void closeServer() {
if (httpServer.IsListening) { if (httpServer.IsListening) {
httpServer.Stop(); reciveT.Abort();
httpServer.Close();
} }
} }
public void RecciveMsg() public void RecciveMsg()
{ {
byte[] receiveBuff = new byte[1024];
int reviceLength = 0;
string msg = ""; string msg = "";
while (httpServer.IsListening) while (httpServer.IsListening)
{ {
try try
{ {
HttpListenerContext context = httpServer.GetContext(); HttpListenerContext context = httpServer.GetContext();
#region 解析收到的消息
HttpListenerRequest request = context.Request; HttpListenerRequest request = context.Request;
// Obtain a response object. switch (request.HttpMethod)
//byte[] buffer = System.Text.Encoding.UTF8.GetBytes(request.Url); {
//reviceLength = tcpClient.Client.Receive(receiveBuff); case "POST":
msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength); {
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 != "") if (msg != "")
{ {
receivedData = msg; receivedData = msg;
isReceivedValue = true; isReceivedValue = true;
} }
#endregion
#region 返回消息
HttpListenerResponse response = context.Response;
// Construct a response.
byte[] buffer = Encoding.UTF8.GetBytes("<HTML><BODY> " + "success" + "</BODY></HTML>");
// 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) catch (Exception e)
{ {

93
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs

@ -6,82 +6,37 @@ using System.Net;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
public class HttpServerView : MonoBehaviour namespace JXSoft {
{ public class HttpServerView : MonoBehaviour
public string[] prefixes;
// Start is called before the first frame update
void Start()
{ {
public string[] prefixes;
} public HttpServerUtility httpServer = new HttpServerUtility();
// Start is called before the first frame update
// Update is called once per frame void Start()
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."); //httpServer.startServer(prefixes);
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. // Update is called once per frame
HttpListener listener = new HttpListener(); void Update()
// Add the prefixes.
foreach (string s in prefixes)
{ {
listener.Prefixes.Add(s); if (httpServer != null && !"".Equals(httpServer.getReceivedValue()))
{
/*GameObject item = Instantiate(tcpMsgItem, tcpMsgContent);
item.GetComponentInChildren<Text>().text = tcpUtil.receivedData;
this.GetModel<TCPClientModel>().onDataRecived.Invoke(tcpUtil.receivedData);
*/
Debug.Log(httpServer.receivedData);
}
} }
listener.Start(); public void startServer() {
Debug.Log("Listening..."); bool isSuccess = httpServer.startServer(prefixes);
// Note: The GetContext method blocks while waiting for a request. Debug.Log("ServerOpen:"+isSuccess);
HttpListenerContext context = listener.GetContext(); }
private void OnDestroy()
#region 解析Request请求
HttpListenerRequest request = context.Request;
string content = "";
switch (request.HttpMethod)
{ {
case "POST": httpServer.closeServer();
{
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("<HTML><BODY> " + request.InputStream + "</BODY></HTML>");
// 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();
} }
} }

Loading…
Cancel
Save