|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
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 == 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 void startServer(string prefix) {
|
|
|
|
string[] prefixes = {prefix};
|
|
|
|
startServer(prefixes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startServer()
|
|
|
|
{
|
|
|
|
string[] prefixes = { address };
|
|
|
|
startServer(prefixes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void closeServer() {
|
|
|
|
if (httpServer.IsListening) {
|
|
|
|
reciveT.Abort();
|
|
|
|
httpServer.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
/// 线程接收到消息后
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>接收到的消息</returns>
|
|
|
|
public string getReceivedValue()
|
|
|
|
{
|
|
|
|
if (isReceivedValue)
|
|
|
|
{
|
|
|
|
isReceivedValue = false;
|
|
|
|
return receivedData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|