|
|
|
@ -6,82 +6,37 @@ using System.Net;
@@ -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>().text = tcpUtil.receivedData; |
|
|
|
|
this.GetModel<TCPClientModel>().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("<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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|