Unity创建简单的Http服务器/客户端测试

服务器部分:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 服务器部分
/// </summary>
public class Sever_Yang : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        IniHttpServer();
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    MyHttpServer HttpServer;
    public void IniHttpServer()
    {
        HttpServer = new MyHttpServer(new string[]
        {
               "http://127.0.0.1:8080/"
        });
        //绑定映射,处理函数
        //当传入URL为"http://127.0.0.1:8080/PostAGVC_Data/"时将调用PostAGVC_Data函数接收和解析
        HttpServer.AddRoute("/PostAGVC_Data/", PostAGVC_Data);
        //当传入URL为"http://127.0.0.1:8080/PostAGV_Data/"时将调用PostAGV_Data函数接收和解析
        HttpServer.AddRoute("/PostAGV_Data/", PostAGV_Data);
        HttpServer.Start();
    }


    public async Task<string> PostAGVC_Data(HttpListenerContext context)
    {
        string httpMethod = context.Request.HttpMethod;
        string responseString = "";
        // 处理 POST 请求
        if (context.Request.HasEntityBody)
        {
            // 从请求主体中获取数据
            using (System.IO.Stream body = context.Request.InputStream)
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(body, context.Request.ContentEncoding))
                {
                    string postData = reader.ReadToEnd(); // 读取 POST 数据
                                                          //处理数据
                    Debug.LogError(postData);                                    //。。。。。。。。。。。。
                                                          //。。。。。。。。。。。。
                                                          //返回字符串
                    responseString = "OK";
                }
            }
        }
        return responseString;
    }

    //与PostAGVC_Data内部类似,处理数据的代码不一样
    public async Task<string> PostAGV_Data(HttpListenerContext context)
    {
        //省略
        Console.WriteLine(111);
        return null;

    }
}


class MyHttpServer
{
    private readonly string mUrl; // 服务器监听的URL
    private readonly HttpListener mListener; // HttpListener实例
    private readonly Dictionary<string, Func<HttpListenerContext, Task<string>>> mRoutes; // 路由映射

    // 构造函数,接收服务器监听地址和端口的数组
    public MyHttpServer(string[] prefixes)
    {
        if (!HttpListener.IsSupported)
        {
            throw new NotSupportedException("HttpListener is not supported on this platform.");
        }

        mListener = new HttpListener(); // 初始化HttpListener实例
        mRoutes = new Dictionary<string, Func<HttpListenerContext, Task<string>>>(); // 初始化路由映射字典

        // 为服务器添加监听地址和端口
        foreach (string prefix in prefixes)
        {
            mListener.Prefixes.Add(prefix);
        }

        mUrl = prefixes[0]; // 记录第一个监听地址
    }

    public bool IsOpen { get { return mListener.IsListening; } }

    // 启动服务器
    public void Start()
    {
        mListener.Start();
        Console.WriteLine($"Server started and listening on {mUrl}");
        mListener.BeginGetContext(ProcessRequestCallback, mListener); // 处理客户端请求
    }

    // 停止服务器
    public void Stop()
    {
        mListener.Stop();
        mListener.Close();
        Console.WriteLine("Server stopped.");
    }

    // 添加路由和处理程序的映射关系
    public void AddRoute(string route, Func<HttpListenerContext, Task<string>> handler)
    {
        mRoutes.Add(route, handler);
    }

    // 处理客户端请求的回调函数
    private async void ProcessRequestCallback(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;

        // 开始下一个请求的监听
        listener.BeginGetContext(ProcessRequestCallback, listener);

        try
        {
            // 获取传入的请求
            HttpListenerContext context = listener.EndGetContext(result);

            // 获取请求方法和URL路径
            string httpMethod = context.Request.HttpMethod;
            string responseString = "No Data!"; // 默认响应字符串
            string url = context.Request.Url.AbsolutePath;
            Func<HttpListenerContext, Task<string>> handler;

            // 如果请求路径存在于路由映射中,执行相应的处理程序
            if (mRoutes.TryGetValue(url, out handler))
            {
                responseString = await handler(context); // 获取处理程序返回的响应数据

                // 将响应数据编码成字节数组
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

                // 设置响应的内容长度和状态码
                context.Response.ContentLength64 = buffer.Length;
                context.Response.StatusCode = (int)HttpStatusCode.OK;

                // 将响应写入输出流并关闭输出流
                context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                context.Response.OutputStream.Close();
            }
            else
            {
                // 如果请求路径不存在于路由映射中,返回404 Not Found
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                context.Response.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error processing request: {ex.Message}");
        }
    }
}


客户端部分:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 客户端部分
/// </summary>
public class MyHttpClient : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        IniHttpClient();
    }
    HttpClient_DP HttpClient;
    public void IniHttpClient()
    {
        if (HttpClient == null)
        {
            HttpClient = new HttpClient_DP();
            HttpClient.TimeOut = TimeSpan.FromSeconds(10);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.LogError("发送baowen ");
            Debug.LogError( HttpClient.Post("http://127.0.0.1:8080/PostAGVC_Data/", "dsadasdfasfas"));
        }
    }
}


public class HttpClient_DP
{
    private readonly HttpClient _client; // HttpClient 实例

    // 构造函数,初始化 HttpClient 实例
    public HttpClient_DP()
    {
        _client = new HttpClient();
    }

    // 设置超时时间
    public TimeSpan TimeOut { set { _client.Timeout = value; } }

    // 发送 GET 请求并返回响应内容
    public async Task<string> Get(string url)
    {
        HttpResponseMessage response = await _client.GetAsync(url); // 发送 GET 请求

        if (response.IsSuccessStatusCode)
        {
            string content = await response.Content.ReadAsStringAsync(); // 读取响应内容
            return content;
        }
        else
        {
            return $"Error: {response.StatusCode}"; // 返回错误信息
        }
    }

    // 发送 POST 请求并返回响应内容
    public async Task<string> Post(string url, string data)
    {
        HttpContent content = new StringContent(data); // 创建包含请求数据的 HttpContent 实例
        Debug.LogError(111);
        HttpResponseMessage response = await _client.PostAsync(url, content); // 发送 POST 请求

        Debug.Log(response.IsSuccessStatusCode);
        if (response.IsSuccessStatusCode)
        {
            string responseData = await response.Content.ReadAsStringAsync(); // 读取响应内容
            return responseData;
        }
        else
        {
            return $"Error: {response.StatusCode}"; // 返回错误信息
        }
    }
}

相关推荐

  1. Unity创建简单Http服务器/客户测试

    2024-06-14 01:16:03       8 阅读
  2. 创建socket服务客户--通信(简单入门)

    2024-06-14 01:16:03       13 阅读
  3. TCP服务器客户创建步骤

    2024-06-14 01:16:03       32 阅读
  4. UDP服务器客户创建步骤

    2024-06-14 01:16:03       21 阅读
  5. Go语言中创建HTTP客户

    2024-06-14 01:16:03       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 01:16:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 01:16:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 01:16:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 01:16:03       18 阅读

热门阅读

  1. 6.13--CSS

    6.13--CSS

    2024-06-14 01:16:03      8 阅读
  2. 【系统学C++】从C语言到C++(三)

    2024-06-14 01:16:03       8 阅读