DESKTOP-B25GA9E\W35
2 years ago
42 changed files with 304 additions and 86 deletions
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2 |
||||
guid: ca6a524def5f09549b607ccfcb8a21ee |
||||
guid: 2eeb0a9ff9746c442a69591d3ba2aa5c |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using UnityEngine; |
||||
using UnityEngine.Events; |
||||
using QFramework; |
||||
using System; |
||||
|
||||
public class UDPMangerArchitecture : Architecture<UDPMangerArchitecture> |
||||
{ |
||||
protected override void Init() |
||||
{ |
||||
this.RegisterUtility(new UDPUtility()); |
||||
this.RegisterModel(new UDPEventModel()); |
||||
} |
||||
} |
||||
public class UDPClientView : MonoBehaviour, IController, ICanSendEvent |
||||
{ |
||||
public UnityEvent onRecievedOpenDevice; |
||||
public UnityEvent onUDPLinkSuccess; |
||||
public UnityEvent onUDPLinkFaild; |
||||
public UnityEvent onServerConnected; |
||||
public UnityEvent onUDPReLink; |
||||
|
||||
private string UDPAddress; |
||||
private int UDPPort; |
||||
private int deviceId; |
||||
// Start is called before the first frame update |
||||
void Awake() |
||||
{ |
||||
initUDPService(); |
||||
DontDestroyOnLoad(this); |
||||
} |
||||
|
||||
public void initUDPService() |
||||
{ |
||||
UDPAddress = "127.0.0.1"; |
||||
UDPPort = 20000; |
||||
deviceId = 1; |
||||
|
||||
GetArchitecture().RegisterEvent<UDPStateChangedEvent>(e => { |
||||
if (e.state == UDPLinkState.Linking) |
||||
{ |
||||
Debug.Log("UDP开始链接"); |
||||
this.GetModel<UDPEventModel>().linkServer(); |
||||
} |
||||
if (e.state == UDPLinkState.LinkSucess) |
||||
{ |
||||
Debug.Log("UDP链接成功"); |
||||
this.GetModel<UDPEventModel>().onReceive<LinkSuccessResponse>(); |
||||
this.GetModel<UDPEventModel>().sendRequestCommand(new LinkUDPRequest(deviceId)); |
||||
onUDPLinkSuccess.Invoke(); |
||||
} |
||||
if (e.state == UDPLinkState.LinkFaild) |
||||
{ |
||||
Debug.Log("UDP连接失败,请联系设备服务管理员"); |
||||
onUDPLinkFaild.Invoke(); |
||||
} |
||||
}); |
||||
|
||||
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e => |
||||
{ |
||||
if (e.res.GetType() == typeof(LinkSuccessResponse)) |
||||
{ |
||||
Debug.Log("Link Server success"); |
||||
this.GetModel<UDPEventModel>().offReceive<LinkSuccessResponse>(); |
||||
onServerConnected.Invoke(); |
||||
} |
||||
}); |
||||
|
||||
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.NoIp); |
||||
this.GetModel<UDPEventModel>().setIP(UDPAddress, UDPPort); |
||||
} |
||||
|
||||
public void restartUDPService() |
||||
{ |
||||
onUDPReLink.Invoke(); |
||||
this.GetModel<UDPEventModel>().closeServer(); |
||||
StartCoroutine(waitTwoSecond()); |
||||
} |
||||
public IEnumerator waitTwoSecond() |
||||
{ |
||||
yield return new WaitForSeconds(2.0f); |
||||
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.Linking); |
||||
} |
||||
public IArchitecture GetArchitecture() |
||||
{ |
||||
return UDPMangerArchitecture.Interface; |
||||
} |
||||
} |
||||
|
||||
|
||||
public class LinkUDPRequest : IRequest |
||||
{ |
||||
public string id; |
||||
public int type; |
||||
public LinkUDPRequest(int id) |
||||
{ |
||||
this.id = id.ToString(); |
||||
this.type = 1; |
||||
} |
||||
public string toJson() |
||||
{ |
||||
return JsonUtility.ToJson(this); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,117 @@
@@ -0,0 +1,117 @@
|
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using UnityEngine; |
||||
using QFramework; |
||||
|
||||
public class UDPEventModel : DataEventModel |
||||
{ |
||||
private UDPLinkState udpState = UDPLinkState.NoIp; |
||||
private string udpAddress = ""; |
||||
private int udpPort = 0; |
||||
public string myAddress = ""; |
||||
protected override void OnInit() |
||||
{ |
||||
this.RegisterEvent<onUDPLinkException>(e => { |
||||
setUDPState(UDPLinkState.LinkTimeOut); |
||||
this.GetUtility<UDPUtility>().CloseUDPClient(); |
||||
}); |
||||
this.RegisterEvent<RequestMsgEvent>(e => { |
||||
if (this.GetUtility<UDPUtility>().isOpenUDP) |
||||
{ |
||||
this.GetUtility<UDPUtility>().sendData(e.req.toJson()); |
||||
} |
||||
else |
||||
{ |
||||
Debug.LogWarning("请先开启UDP链接"); |
||||
} |
||||
}); |
||||
} |
||||
public void setUDPState(UDPLinkState state) |
||||
{ |
||||
this.udpState = state; |
||||
this.SendEvent(new UDPStateChangedEvent(state)); |
||||
} |
||||
public UDPLinkState getState() |
||||
{ |
||||
return this.udpState; |
||||
} |
||||
/// <summary> |
||||
/// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器) |
||||
/// </summary> |
||||
/// <param name="ip">ip地址</param> |
||||
/// <param name="port">端口号</param> |
||||
public void setIP(string ip, int port) |
||||
{ |
||||
this.udpAddress = ip; |
||||
this.udpPort = port; |
||||
//此处可以加ip校验 |
||||
Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加"); |
||||
setUDPState(UDPLinkState.Linking); |
||||
} |
||||
/// <summary> |
||||
/// 与服务端建立链接 |
||||
/// </summary> |
||||
public void linkServer() |
||||
{ |
||||
if (udpState == UDPLinkState.Linking) |
||||
{ |
||||
if (!this.GetUtility<UDPUtility>().isOpenUDP) |
||||
{ |
||||
|
||||
bool isSuccess = this.GetUtility<UDPUtility>().StartUDPClient(udpAddress, udpPort); |
||||
if (isSuccess) |
||||
{ |
||||
setUDPState(UDPLinkState.LinkSucess); |
||||
} |
||||
else |
||||
{ |
||||
setUDPState(UDPLinkState.LinkFaild); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
/// <summary> |
||||
/// 与服务端断开链接 |
||||
/// </summary> |
||||
public void closeServer() |
||||
{ |
||||
if (udpState == UDPLinkState.LinkSucess) |
||||
{ |
||||
if (this.GetUtility<UDPUtility>().isOpenUDP) |
||||
{ |
||||
|
||||
this.GetUtility<UDPUtility>().CloseUDPClient(); |
||||
setUDPState(UDPLinkState.NoIp); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
#region enum |
||||
public enum UDPLinkState |
||||
{ |
||||
NoIp = 0, |
||||
Linking = 1, |
||||
LinkFaild = 2, |
||||
LinkSucess = 3, |
||||
LinkTimeOut = 4 |
||||
} |
||||
#endregion |
||||
|
||||
#region event |
||||
public struct UDPStateChangedEvent |
||||
{ |
||||
public UDPLinkState state; |
||||
public UDPStateChangedEvent(UDPLinkState state_) |
||||
{ |
||||
state = state_; |
||||
} |
||||
} |
||||
public struct onUDPLinkException |
||||
{ |
||||
public string exceptionMsg; |
||||
public onUDPLinkException(string exceptionMsg_) |
||||
{ |
||||
exceptionMsg = exceptionMsg_; |
||||
} |
||||
} |
||||
#endregion |
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2 |
||||
guid: b55fec30f758e8d43ac35ea3444023ec |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using System.Net; |
||||
using System.Net.Sockets; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using UnityEngine; |
||||
using UnityEngine.UI; |
||||
using QFramework; |
||||
|
||||
public class UDPPrinter : MonoBehaviour, IController, ICanSendEvent, ICanGetUtility |
||||
{ |
||||
private UDPUtility udpUtil; |
||||
public Transform udpMsgContent; |
||||
public GameObject udpMsgItem; |
||||
|
||||
public InputField InputSendMsg; |
||||
private bool isUDPInit; |
||||
// Start is called before the first frame update |
||||
void Start() |
||||
{ |
||||
udpUtil = GetArchitecture().GetUtility<UDPUtility>(); |
||||
} |
||||
|
||||
// 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.Value; |
||||
this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData.Value); |
||||
} |
||||
|
||||
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP) |
||||
{ |
||||
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData.Value)); |
||||
udpUtil.isOpenUDP.Value = 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 IArchitecture GetArchitecture() |
||||
{ |
||||
return UDPMangerArchitecture.Interface; |
||||
} |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 53ce428612aa35f43b5b3ea3aa4ce9b8 |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -1,17 +0,0 @@
@@ -1,17 +0,0 @@
|
||||
|
||||
using UnityEngine; |
||||
|
||||
public class UDPClientView : MonoBehaviour |
||||
{ |
||||
// Start is called before the first frame update |
||||
void Start() |
||||
{ |
||||
|
||||
} |
||||
|
||||
// Update is called once per frame |
||||
void Update() |
||||
{ |
||||
|
||||
} |
||||
} |
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using System.Net; |
||||
using System.Net.Sockets; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using UnityEngine; |
||||
|
||||
public class UDPPrinter : MonoBehaviour |
||||
{ |
||||
private UDPUtility udpUtil; |
||||
// Start is called before the first frame update |
||||
void Start() |
||||
{ |
||||
udpUtil = new UDPUtility("192.168.1.41", 20000); |
||||
udpUtil.sendData("hello everyone!"); |
||||
} |
||||
|
||||
// Update is called once per frame |
||||
void Update() |
||||
{ |
||||
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue())) |
||||
{ |
||||
Debug.Log(udpUtil.receivedData.Value); |
||||
} |
||||
} |
||||
private void OnDestroy() |
||||
{ |
||||
if (udpUtil.reciveT != null && udpUtil.reciveT.ThreadState == ThreadState.Running) |
||||
{ |
||||
udpUtil.reciveT.Abort(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue