Browse Source

添加了json格式判断

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
e587a864c3
  1. 5
      Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs
  2. 20
      Assets/MsgTransmitTools/Example/Script/UDPExample.cs
  3. 46
      Assets/MsgTransmitTools/src/DataEventModel.cs
  4. 1
      Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs
  5. 17
      README.md

5
Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs

@ -48,11 +48,6 @@ public struct YourNumberFilterResponse : IResponse
} }
return isNumber; return isNumber;
} }
public string getException()
{
return exceptionMsg;
}
} }
public class YourRequest:IRequest { public class YourRequest:IRequest {

20
Assets/MsgTransmitTools/Example/Script/UDPExample.cs

@ -23,15 +23,20 @@ public class UDPExample : MonoBehaviour,IUDPClient
} }
} }
public class UResponseTest:AbstractJsonResponse { public struct UResponseTest:IResponse {
public string msg; public string msg;
public UResponseTest() { } public bool trySetData(string protolData)
public override bool trySetData(string json)
{ {
try try
{ {
UResponseTest res = JsonMapper.ToObject<UResponseTest>(json); UResponseTest res = JsonMapper.ToObject<UResponseTest>(protolData);
msg = res.msg; if (!this.isTypeOfJson(protolData))
{
return false;
}
else {
this.msg = res.msg;
}
return true; return true;
} }
catch (Exception e) { catch (Exception e) {
@ -39,4 +44,9 @@ public class UResponseTest:AbstractJsonResponse {
return false; return false;
} }
} }
public string toProtocolData()
{
return "";
}
} }

46
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -19,7 +19,7 @@ namespace JXSoft {
public Color printColor = Color.white; public Color printColor = Color.white;
#region ReceiveFunctions #region ReceiveFunctions
public void receiveResponse<T>(string json) where T : IResponse, new() public void receiveResponse<T>(string protocolData) where T : IResponse, new()
{ {
//如果找不到对应命令,则添加命令 //如果找不到对应命令,则添加命令
if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null) if (mCommandContainer.Get<ExcuteResponseCommand<T>>() == null)
@ -27,7 +27,7 @@ namespace JXSoft {
mCommandContainer.Register(new ExcuteResponseCommand<T>(new T())); mCommandContainer.Register(new ExcuteResponseCommand<T>(new T()));
} }
//找到命令后添加命令 //找到命令后添加命令
mCommandContainer.Get<ExcuteResponseCommand<T>>().setJson(json); mCommandContainer.Get<ExcuteResponseCommand<T>>().setProtocolData(protocolData);
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>()); this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>());
} }
/// <summary> /// <summary>
@ -97,7 +97,7 @@ namespace JXSoft {
/// <typeparam name="TResponse">响应数据格式</typeparam> /// <typeparam name="TResponse">响应数据格式</typeparam>
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse
{ {
public string json; public string protocolData;
private TResponse response; private TResponse response;
private Color printColor = Color.white; private Color printColor = Color.white;
public ExcuteResponseCommand(TResponse response) public ExcuteResponseCommand(TResponse response)
@ -109,7 +109,7 @@ namespace JXSoft {
} }
protected override void OnExecute() protected override void OnExecute()
{ {
bool isSet = response.trySetData(json); bool isSet = response.trySetData(protocolData);
if (isSet) if (isSet)
{ {
if (response.toProtocolData() != "") { if (response.toProtocolData() != "") {
@ -118,9 +118,9 @@ namespace JXSoft {
this.SendEvent(new ResponseMsgEvent(response)); this.SendEvent(new ResponseMsgEvent(response));
} }
} }
public void setJson(string json) public void setProtocolData(string protocolData)
{ {
this.json = json; this.protocolData = protocolData;
} }
} }
@ -150,7 +150,7 @@ namespace JXSoft {
public interface IResponse public interface IResponse
{ {
/// <summary> /// <summary>
/// 此处用于截获接收到的数据,建议作为调试依据 /// 可以用于打印调试信息
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
string toProtocolData(); string toProtocolData();
@ -244,6 +244,38 @@ namespace JXSoft {
}); });
} }
} }
/// <summary>
/// 用于扩展类型结构
/// </summary>
public static class IResponseExtention {
public static bool isTypeOfJson(this IResponse self, string json)
{
JsonData protocolData = new JsonData();
try
{
protocolData = JsonMapper.ToObject(json);
}
catch {
return false;
}
JsonData classData = JsonMapper.ToObject(JsonMapper.ToJson(self));
if (protocolData.Keys.Count != classData.Keys.Count)
{
return false;
}
foreach (string key in classData.Keys)
{
if (!protocolData.ContainsKey(key))
{
return false;
}
}
return true;
}
}
#endregion #endregion
#region event #region event

1
Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs

@ -48,5 +48,6 @@ namespace JXSoft {
public static TObject toObjectByJson<TObject>(this string self) { public static TObject toObjectByJson<TObject>(this string self) {
return JsonMapper.ToObject<TObject>(self); return JsonMapper.ToObject<TObject>(self);
} }
} }
} }

17
README.md

@ -28,8 +28,6 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的
详情参考 [UdpClient 类官方说明](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.udpclient?view=netframework-4.8) 详情参考 [UdpClient 类官方说明](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.udpclient?view=netframework-4.8)
目前支持的协议格式一览,后续会增加更多协议格式转换 目前支持的协议格式一览,后续会增加更多协议格式转换
| Protocol | | Protocol |
@ -40,10 +38,6 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的
协议转换方案后续由协议API提供参考跟封装(此处用了litjson作为协议转换方案)。 协议转换方案后续由协议API提供参考跟封装(此处用了litjson作为协议转换方案)。
# 使用方法 # 使用方法
## 1.定义响应&请求消息结构 ## 1.定义响应&请求消息结构
@ -66,9 +60,7 @@ UDP虽然是无连接协议,但通讯包是基于UDPClient这个类编写的
```cs ```cs
public struct YourNumberFilterResponse : IResponse public struct YourNumberFilterResponse : IResponse
{ {
public string code; public int codeNum;
private int codeNum;
private string exceptionMsg;
public string toProtocolData() public string toProtocolData()
{ {
return "你想要打印的调试信息,无需打印返回空字符串即可"; return "你想要打印的调试信息,无需打印返回空字符串即可";
@ -76,18 +68,13 @@ public struct YourNumberFilterResponse : IResponse
public bool trySetData(string protocolData) public bool trySetData(string protocolData)
{ {
bool isNumber = int.TryParse(code, out codeNum); bool isNumber = int.TryParse(protocolData, out codeNum);
if (!isNumber) if (!isNumber)
{ {
exceptionMsg = "这不是数字"; exceptionMsg = "这不是数字";
} }
return isNumber; return isNumber;
} }
public string getException()
{
return exceptionMsg;
}
} }
``` ```

Loading…
Cancel
Save