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. 79
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs

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

@ -667,7 +667,7 @@ MonoBehaviour: @@ -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}

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

@ -6,6 +6,7 @@ using System.Net; @@ -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 { @@ -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 { @@ -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 { @@ -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("<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)
{

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

@ -6,82 +6,37 @@ using System.Net; @@ -6,82 +6,37 @@ using System.Net;
using System.Text;
using UnityEngine;
public class HttpServerView : MonoBehaviour
{
namespace JXSoft {
public class HttpServerView : MonoBehaviour
{
public string[] prefixes;
public HttpServerUtility httpServer = new HttpServerUtility();
// Start is called before the first frame update
void Start()
{
//httpServer.startServer(prefixes);
}
// 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)
if (httpServer != null && !"".Equals(httpServer.getReceivedValue()))
{
Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
/*GameObject item = Instantiate(tcpMsgItem, tcpMsgContent);
item.GetComponentInChildren<Text>().text = tcpUtil.receivedData;
this.GetModel<TCPClientModel>().onDataRecived.Invoke(tcpUtil.receivedData);
*/
Debug.Log(httpServer.receivedData);
}
// 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);
public void startServer() {
bool isSuccess = httpServer.startServer(prefixes);
Debug.Log("ServerOpen:"+isSuccess);
}
break;
case "GET":
private void OnDestroy()
{
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("<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