DESKTOP-B25GA9E\W35
2 years ago
7 changed files with 260 additions and 2 deletions
@ -0,0 +1,135 @@
@@ -0,0 +1,135 @@
|
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using UnityEngine; |
||||
using System; |
||||
using System.Net; |
||||
using System.Threading; |
||||
using QFrameworkCP; |
||||
using System.Text; |
||||
|
||||
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.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(); |
||||
Debug.Log("Listening..."); |
||||
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) { |
||||
httpServer.Stop(); |
||||
} |
||||
} |
||||
|
||||
public void RecciveMsg() |
||||
{ |
||||
byte[] receiveBuff = new byte[1024]; |
||||
int reviceLength = 0; |
||||
string msg = ""; |
||||
while (httpServer.IsListening) |
||||
{ |
||||
try |
||||
{ |
||||
HttpListenerContext context = httpServer.GetContext(); |
||||
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); |
||||
if (msg != "") |
||||
{ |
||||
receivedData = msg; |
||||
isReceivedValue = true; |
||||
} |
||||
} |
||||
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 ""; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2 |
||||
guid: bb431867bcae5b949a6115a944655b4e |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,87 @@
@@ -0,0 +1,87 @@
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
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() |
||||
{ |
||||
|
||||
} |
||||
|
||||
// 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) |
||||
{ |
||||
Debug.Log("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); |
||||
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. |
||||
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); |
||||
} |
||||
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(); |
||||
} |
||||
} |
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2 |
||||
guid: df3c647a96e64d14f9aedcd84c2b3433 |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
Loading…
Reference in new issue