金铉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.

110 lines
3.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using QFrameworkCP;
using System;
2 years ago
using UnityEngine.UI;
using System.Threading;
namespace JXSoft {
public class UDPMangerArchitecture : Architecture<UDPMangerArchitecture>
{
protected override void Init()
{
this.RegisterUtility(new UDPUtility());
this.RegisterModel(new UDPClientModel());
}
}
public class UDPClientView : MonoBehaviour, IController
{
private string UDPAddress;
private int UDPPort;
2 years ago
private UDPUtility udpUtil;
public Transform udpMsgContent;
public GameObject udpMsgItem;
public InputField InputSendMsg;
2 years ago
void Start()
{
udpUtil = GetArchitecture().GetUtility<UDPUtility>();
initUDPService();
2 years ago
}
// Update is called once per frame
void Update()
{
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue()))
{
GameObject item = Instantiate(udpMsgItem, udpMsgContent);
item.GetComponentInChildren<Text>().text = udpUtil.receivedData + udpUtil.getReceivedAddress();
2 years ago
this.GetModel<UDPClientModel>().onDataRecived.Invoke(udpUtil.receivedData);
}
}
private void OnDestroy()
{
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running)
{
udpUtil.reciveT.Abort();
}
}
public void sendMsg()
{
this.GetUtility<UDPUtility>().sendData(InputSendMsg.text,"192.168.1.41", 8443);
2 years ago
}
public void initUDPService()
{
UDPAddress = "192.168.1.41";
UDPPort = 20000;
this.GetModel<UDPClientModel>().setIP(UDPAddress, UDPPort);
this.GetModel<UDPClientModel>().Start();
}
public void restartUDPService()
{
this.GetModel<UDPClientModel>().Close();
StartCoroutine(waitTwoSecond());
}
public IEnumerator waitTwoSecond()
{
yield return new WaitForSeconds(2.0f);
this.GetModel<UDPClientModel>().Start();
}
public IArchitecture GetArchitecture()
{
return UDPMangerArchitecture.Interface;
}
}
public interface IUDPClient
{
}
public static class UDPClientExtention
{
public static IUnRegister RegisterMessageEvent<TResponse>(this IUDPClient self, Action<TResponse> onEvent) where TResponse : IResponse
{
return UDPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
if (e.res.GetType() == typeof(TResponse))
{
onEvent.Invoke((TResponse)e.res);
}
});
}
public static void onReceive<TResponse>(this IUDPClient self) where TResponse : IResponse, new()
{
UDPMangerArchitecture.Interface.GetModel<UDPClientModel>().onReceive<TResponse>();
}
public static void offReceive<TResponse>(this IUDPClient self) where TResponse : IResponse, new()
{
UDPMangerArchitecture.Interface.GetModel<UDPClientModel>().offReceive<TResponse>();
}
public static void sendRequest<TRequest>(this IUDPClient self, TRequest request) where TRequest : IRequest, new()
{
UDPMangerArchitecture.Interface.GetModel<UDPClientModel>().sendRequest(request);
}
}
}