|
|
|
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<UDPMangerArchitecture>
|
|
|
|
{
|
|
|
|
protected override void Init()
|
|
|
|
{
|
|
|
|
this.RegisterUtility(new UDPUtility());
|
|
|
|
this.RegisterModel(new UDPClientModel());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public class UDPClientView : MonoBehaviour, IController
|
|
|
|
{
|
|
|
|
public UnityEvent onRecievedOpenDevice;
|
|
|
|
public UnityEvent onUDPLinkSuccess;
|
|
|
|
public UnityEvent onUDPLinkFaild;
|
|
|
|
public UnityEvent onServerConnected;
|
|
|
|
public UnityEvent onUDPReLink;
|
|
|
|
|
|
|
|
private string UDPAddress;
|
|
|
|
private int UDPPort;
|
|
|
|
private UDPUtility udpUtil;
|
|
|
|
public Transform udpMsgContent;
|
|
|
|
public GameObject udpMsgItem;
|
|
|
|
public InputField InputSendMsg;
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
udpUtil = GetArchitecture().GetUtility<UDPUtility>();
|
|
|
|
initUDPService();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
this.GetModel<UDPClientModel>().onDataRecived.Invoke(udpUtil.receivedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP)
|
|
|
|
{
|
|
|
|
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData));
|
|
|
|
udpUtil.isOpenUDP = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running)
|
|
|
|
{
|
|
|
|
udpUtil.reciveT.Abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void sendMsg()
|
|
|
|
{
|
|
|
|
this.GetUtility<UDPUtility>().sendData(InputSendMsg.text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initUDPService()
|
|
|
|
{
|
|
|
|
UDPAddress = "192.168.1.41";
|
|
|
|
UDPPort = 52101;
|
|
|
|
this.RegisterLinkStateEvent((int)UDPLinkState.Linking, () => {
|
|
|
|
Debug.Log("UDP开始链接");
|
|
|
|
this.GetModel<UDPClientModel>().linkServer();
|
|
|
|
});
|
|
|
|
this.RegisterLinkStateEvent((int)UDPLinkState.LinkSucess, () =>
|
|
|
|
{
|
|
|
|
Debug.Log("UDP链接成功");
|
|
|
|
onUDPLinkSuccess.Invoke();
|
|
|
|
});
|
|
|
|
this.RegisterLinkStateEvent((int)UDPLinkState.LinkFaild, () =>
|
|
|
|
{
|
|
|
|
Debug.Log("UDP连接失败,请联系设备服务管理员");
|
|
|
|
onUDPLinkFaild.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.GetModel<UDPClientModel>().setIP(UDPAddress, UDPPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void restartUDPService()
|
|
|
|
{
|
|
|
|
onUDPReLink.Invoke();
|
|
|
|
this.GetModel<UDPClientModel>().closeServer();
|
|
|
|
StartCoroutine(waitTwoSecond());
|
|
|
|
}
|
|
|
|
public IEnumerator waitTwoSecond()
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(2.0f);
|
|
|
|
this.GetModel<UDPClientModel>().setLinkState((int)UDPLinkState.Linking);
|
|
|
|
}
|
|
|
|
public IArchitecture GetArchitecture()
|
|
|
|
{
|
|
|
|
return UDPMangerArchitecture.Interface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|