|
|
|
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
|
|
|
|
{
|
|
|
|
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 + udpUtil.getReceivedAddress();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|