This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -1,80 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
using Cysharp.Threading.Tasks;
using BITKit.HttpNet.Model;
using System.Linq;
namespace BITKit.HttpNet
{
public class NetworkListener : MonoBehaviour
{
public int port = 7001;
public Provider output;
HttpListener listener = new();
bool isEnabled;
void OnEnable()
{
isEnabled = true;
}
void OnDisable()
{
isEnabled = false;
listener.Stop();
}
void Start()
{
Excute();
}
async void Excute()
{
if (!HttpListener.IsSupported)
{
Debug.Log("HttpListener is not supported this platform");
return;
}
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
listener.Prefixes.Add($"http://+:{port}/");
var ticker = await DI.GetAsync<IThreadTicker>();
ticker.Add(Listen);
}
void Listen()
{
listener.Start();
while (isEnabled)
{
var result = listener.BeginGetContext(ListenerCallback, listener);
result.AsyncWaitHandle.WaitOne();
}
}
private void ListenerCallback(IAsyncResult result)
{
var context = listener.EndGetContext(result);
var request = context.Request;
var response = context.Response;
var output = response.OutputStream;
var responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
try
{
this.output?.Set(request);
responseString = BITModel.ContextModel.Get("success");
}
catch (System.Exception e)
{
responseString = BITModel.ContextModel.Error(e);
Debug.LogError(e);
}
var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
output.Write(buffer);
output.Close();
response.Close();
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 259562ab3c5f28243819c92d2db08104
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: