You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.9 KiB
187 lines
5.9 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using System; |
|
using System.Net; |
|
using System.Threading; |
|
using QFrameworkCP; |
|
using System.Text; |
|
using System.IO; |
|
|
|
namespace JXSoft { |
|
public class HttpServerModel : DataEventModel |
|
{ |
|
public LinkStateDefault httpServerState = LinkStateDefault.Close; |
|
protected override void OnInit() |
|
{ |
|
|
|
} |
|
public override void setLinkState(int linkState) |
|
{ |
|
this.httpServerState = (LinkStateDefault)linkState; |
|
} |
|
|
|
public override int getLinkState() |
|
{ |
|
return (int)httpServerState; |
|
} |
|
public override bool Start() |
|
{ |
|
bool result = this.GetUtility<HttpServerUtility>().startServer(); |
|
if (result) { |
|
setLinkState((int)LinkStateDefault.Open); |
|
} |
|
return result; |
|
} |
|
public override bool Close() |
|
{ |
|
this.GetUtility<HttpServerUtility>().closeServer(); |
|
setLinkState((int)LinkStateDefault.Close); |
|
return true; |
|
} |
|
|
|
public void setPrefixes(string[] prefixes) { |
|
this.GetUtility<HttpServerUtility>().address = prefixes; |
|
} |
|
} |
|
|
|
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; |
|
|
|
#region OpenServer |
|
public bool startServer(string[] prefixes) { |
|
if (httpServer == null || !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(); |
|
reciveT = new Thread(RecciveMsg); |
|
reciveT.IsBackground = true; |
|
reciveT.Start(); |
|
return true; |
|
} |
|
return false; |
|
} |
|
public bool startServer(string prefix) { |
|
string[] prefixes = {prefix}; |
|
bool isSuccess = startServer(prefixes); |
|
return isSuccess; |
|
} |
|
public bool startServer() |
|
{ |
|
bool isSuccess = startServer(address); |
|
return isSuccess; |
|
} |
|
#endregion |
|
|
|
#region CloseServer |
|
public void closeServer() { |
|
if (httpServer == null) { |
|
return; |
|
} |
|
if (httpServer.IsListening) { |
|
reciveT.Abort(); |
|
httpServer.Close(); |
|
} |
|
} |
|
#endregion |
|
|
|
#region MessageReceive |
|
public void RecciveMsg() |
|
{ |
|
string msg = ""; |
|
while (httpServer.IsListening) |
|
{ |
|
try |
|
{ |
|
HttpListenerContext context = httpServer.GetContext(); |
|
#region 解析收到的消息 |
|
HttpListenerRequest request = context.Request; |
|
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) |
|
{ |
|
Debug.LogWarning(e); |
|
//断线发送异常 |
|
exceptionData = e.ToString(); |
|
break; |
|
} |
|
} |
|
Debug.Log("------------end While-------------"); |
|
} |
|
|
|
/// <summary> |
|
/// Update调用,主线程信息提取 |
|
/// </summary> |
|
/// <returns>接收到的消息</returns> |
|
public string getReceivedValue() |
|
{ |
|
if (isReceivedValue) |
|
{ |
|
isReceivedValue = false; |
|
return receivedData; |
|
} |
|
else |
|
{ |
|
return ""; |
|
} |
|
} |
|
#endregion |
|
} |
|
} |