金铉Unity插件库 Unity版本2018.4.32f 目前包含本地化存储功能(根据类名存储读写),TCP客户端监听类型功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
using QFrameworkCP;
namespace JXSoft {
public class HttpServerArchitecture : Architecture<HttpServerArchitecture>
{
protected override void Init()
{
this.RegisterUtility(new HttpServerUtility());
this.RegisterModel(new HttpServerModel());
}
}
public class HttpServerView : MonoBehaviour,IController
{
[Header("WebUrl")]
public string[] prefixes;
// Start is called before the first frame update
void Start()
{
startServer();
}
// Update is called once per frame
void Update()
{
if (this.GetUtility<HttpServerUtility>().httpServer != null && !"".Equals(this.GetUtility<HttpServerUtility>().getReceivedValue()))
{
this.GetModel<HttpServerModel>().onDataRecived.Invoke(this.GetUtility<HttpServerUtility>().receivedData);
}
}
public void startServer() {
this.GetModel<HttpServerModel>().setPrefixes(prefixes);
bool isSuccess = this.GetModel<HttpServerModel>().Start();
Debug.Log("ServerOpen:"+isSuccess);
}
private void OnDestroy()
{
this.GetModel<HttpServerModel>().Close();
}
public IArchitecture GetArchitecture()
{
return HttpServerArchitecture.Interface;
}
}
public interface IHttpServer
{
}
public static class HttpServerExtention {
public static IUnRegister RegisterMessageEvent<TResponse>(this IHttpServer self, Action<TResponse> onEvent) where TResponse : IResponse
{
return HttpServerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
if (e.res.GetType() == typeof(TResponse))
{
onEvent.Invoke((TResponse)e.res);
}
});
}
public static void onReceive<TResponse>(this IHttpServer self) where TResponse : IResponse,new()
{
HttpServerArchitecture.Interface.GetModel<HttpServerModel>().onReceive<TResponse>();
}
public static void offReceive<TResponse>(this IHttpServer self) where TResponse : IResponse, new()
{
HttpServerArchitecture.Interface.GetModel<HttpServerModel>().offReceive<TResponse>();
}
}
}