Browse Source

新增了httpserver

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
eb24450089
  1. 17
      Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity
  2. 0
      Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity.meta
  3. 135
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs
  4. 11
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta
  5. 87
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs
  6. 11
      Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta
  7. 1
      Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs

17
Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity → Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity

@ -400,9 +400,11 @@ MonoBehaviour: @@ -400,9 +400,11 @@ MonoBehaviour:
m_GameObject: {fileID: 1020834817}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d21a45ed4d4194444984809e9676e7fb, type: 3}
m_Script: {fileID: 11500000, guid: df3c647a96e64d14f9aedcd84c2b3433, type: 3}
m_Name:
m_EditorClassIdentifier:
prefixes:
- http://192.168.1.41:8443/index/
--- !u!4 &1020834819
Transform:
m_ObjectHideFlags: 0
@ -663,7 +665,18 @@ MonoBehaviour: @@ -663,7 +665,18 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 2100516202}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 1020834818}
m_MethodName: startListen
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &2100516202
MonoBehaviour:
m_ObjectHideFlags: 0

0
Assets/MsgTransmitTools/Example/Scenes/TCPServerExample.unity.meta → Assets/MsgTransmitTools/Example/Scenes/ServerExample.unity.meta

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

@ -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 "";
}
}
}
}

11
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerModel.cs.meta

@ -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:

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

@ -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();
}
}

11
Assets/MsgTransmitTools/ExtendLinkModel/HttpServer/HttpServerView.cs.meta

@ -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:

1
Assets/MsgTransmitTools/ExtendLinkModel/TCPServer/Script/SocketServer.cs

@ -15,6 +15,7 @@ public class SocketServer : MonoBehaviour @@ -15,6 +15,7 @@ public class SocketServer : MonoBehaviour
string _ip = "192.168.1.41";
Thread thread;
void Start()
{
StartServer();

Loading…
Cancel
Save