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