using System.Collections; using UnityEngine; using UnityEngine.Events; using QFrameworkCP; using System.Threading; using UnityEngine.UI; using System; namespace JXSoft { public class TCPMangerArchitecture : Architecture { protected override void Init() { this.RegisterModel(new TCPClientModel()); this.RegisterModel(new TCPClient2Model()); } } public class TCPClient2Model :TCPClientModel{ public override string getSender() { return "TCP2ºÅ»ú"; } } public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent { public string tcpAddress; public int tcpPort; //Printer&MsgGetter public Transform tcpMsgContent; public GameObject tcpMsgItem; // Start is called before the first frame update void Awake() { initTCPService(); } public void initTCPService() { this.GetModel().setIP(tcpAddress, tcpPort); this.GetModel().Start(); this.GetModel().setIP(tcpAddress, tcpPort+1); this.GetModel().Start(); } #region ÖØÆôÁ´½ÓʾÀý public void restartTCPService() { this.GetModel().Close(); StartCoroutine(waitTwoSecond()); } public IEnumerator waitTwoSecond() { yield return new WaitForSeconds(2.0f); this.GetModel().Start(); } #endregion // Update is called once per frame void Update() { //Debug.Log(tcpUtil.Read_TCPClient()); recevieMsgUpdate(); recevieMsgUpdate(); } public void recevieMsgUpdate() where T:TCPClientModel { if (this.GetModel().getInstance() != null && !"".Equals(this.GetModel().getInstance().getReceivedValue())) { GameObject item = Instantiate(tcpMsgItem, tcpMsgContent); item.GetComponentInChildren().text = this.GetModel().getInstance().receivedData; this.GetModel().onDataRecived.Invoke(this.GetModel().getInstance().receivedData); } if (this.GetModel().getInstance().getTimeOutState() && this.GetModel().getInstance().isOpenTCP) { this.SendEvent(new onLinkException(this.GetModel().getInstance().exceptionData)); this.GetModel().getInstance().isOpenTCP = false; } } private void OnDestroy() { this.GetModel().Close(); this.GetModel().Close(); } public IArchitecture GetArchitecture() { return TCPMangerArchitecture.Interface; } } public interface ITCPClient { } public static class TCPClientExtention { public static IUnRegister RegisterMessageEvent(this ITCPClient self, Action onEvent) where TResponse : IResponse { return TCPMangerArchitecture.Interface.RegisterEvent(e => { if (e.res.GetType() == typeof(TResponse)) { onEvent.Invoke((TResponse)e.res); } }); } public static IUnRegister RegisterMessageEvent(this ITCPClient self, Action onEvent) where TResponse : IResponse { return TCPMangerArchitecture.Interface.RegisterEvent(e => { if (e.res.GetType() == typeof(TResponse)) { onEvent.Invoke((TResponse)e.res,e.sender); } }); } public static void onReceive(this ITCPClient self) where TResponse : IResponse, new() { TCPMangerArchitecture.Interface.GetModel().onReceive(); } public static void onReceive(this ITCPClient self) where TResponse : IResponse, new()where TTCPClientModel:TCPClientModel { TCPMangerArchitecture.Interface.GetModel().onReceive(); } public static void offReceive(this ITCPClient self) where TResponse : IResponse, new() { TCPMangerArchitecture.Interface.GetModel().offReceive(); } public static void offReceive(this ITCPClient self) where TResponse : IResponse, new() where TTCPClientModel : TCPClientModel { TCPMangerArchitecture.Interface.GetModel().offReceive(); } public static void sendRequest(this ITCPClient self ,TRequest request) where TRequest : IRequest, new() { TCPMangerArchitecture.Interface.GetModel().sendRequest(request); } public static void sendRequest(this ITCPClient self, TRequest request) where TRequest : IRequest, new() where TDataModel:DataEventModel { TCPMangerArchitecture.Interface.GetModel().sendRequest(request); } } }