金铉Unity插件库 Unity版本2018.4.32f 目前包含本地化存储功能(根据类名存储读写),TCP客户端监听类型功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

2 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using QFramework;
using TCPClientTools;
using System;
2 years ago
public class TCPMangerArchitecture : Architecture<TCPMangerArchitecture>
{
protected override void Init()
{
this.RegisterUtility(new TCPUtility());
this.RegisterModel(new TCPEventModel());
}
}
2 years ago
public class TCPClientView : MonoBehaviour,IController,ICanSendEvent
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onTCPLinkSuccess;
public UnityEvent onTCPLinkFaild;
public UnityEvent onServerConnected;
public UnityEvent onTCPReLink;
private string tcpAddress;
private int tcpPort;
private int deviceId;
// Start is called before the first frame update
void Awake()
{
initTCPService();
DontDestroyOnLoad(this);
}
public void initTCPService() {
tcpAddress = "127.0.0.1";
tcpPort = 20000;
2 years ago
deviceId = 1;
GetArchitecture().RegisterEvent<TCPStateChangedEvent>(e => {
if (e.state == TCPLinkState.Linking)
{
Debug.Log("TCP<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
this.GetModel<TCPEventModel>().linkServer();
}
if (e.state == TCPLinkState.LinkSucess)
{
Debug.Log("TCP<EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>");
this.GetModel<TCPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<TCPEventModel>().sendRequestCommand(new LinkTCPRequest(deviceId));
onTCPLinkSuccess.Invoke();
}
if (e.state == TCPLinkState.LinkFaild)
{
Debug.Log("TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա");
onTCPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e =>
{
if (e.res.GetType() == typeof(LinkSuccessResponse))
{
Debug.Log("Link Server success");
this.GetModel<TCPEventModel>().offReceive<LinkSuccessResponse>();
onServerConnected.Invoke();
}
});
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.NoIp);
this.GetModel<TCPEventModel>().setIP(tcpAddress, tcpPort);
}
public void restartTCPService() {
onTCPReLink.Invoke();
this.GetModel<TCPEventModel>().closeServer();
StartCoroutine(waitTwoSecond());
}
public IEnumerator waitTwoSecond() {
yield return new WaitForSeconds(2.0f);
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.Linking);
}
public IArchitecture GetArchitecture()
{
return TCPMangerArchitecture.Interface;
}
}
public class LinkSuccessResponse : AbstractResponse
2 years ago
{
public string code;
public string data;
public string msg;
public LinkSuccessResponse() {
this.code = "";
this.data = "";
this.msg = "";
}
}
public class LinkTCPRequest :IRequest{
2 years ago
public string id;
public int type;
public LinkTCPRequest(int id) {
this.id = id.ToString();
this.type = 1;
}
public string toJson()
{
return JsonUtility.ToJson(this);
}
}