Browse Source

TCP通讯包增加了Sender获取的注册

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
f010419ce9
  1. 3
      Assets/MsgTransmitTools/Example/Script/TCPClientExample.cs
  2. 4
      Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs
  3. 4
      Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs
  4. 15
      Assets/MsgTransmitTools/src/DataEventModel.cs

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

@ -9,8 +9,9 @@ public class TCPClientExample : MonoBehaviour,ITCPClient
void Start() void Start()
{ {
//注册接收到数据类型事件监听 //注册接收到数据类型事件监听
this.RegisterMessageEvent<YourResponse>(e => { this.RegisterMessageEvent<YourResponse>((e,sender) => {
Debug.Log("TestGetMsg:"+e.msg); Debug.Log("TestGetMsg:"+e.msg);
Debug.Log("IpAddress:"+sender);
//应答 //应答
this.sendRequest(new StringRequest("Received!")); this.sendRequest(new StringRequest("Received!"));
//取消接收该数据 //取消接收该数据

4
Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientModel.cs

@ -105,6 +105,10 @@ namespace JXSoft
} }
return false; return false;
} }
public override string getSender()
{
return tcpAddress;
}
} }
#region enum #region enum

4
Assets/MsgTransmitTools/ExtendLinkModel/TCPClient/Script/TCPClientView.cs

@ -107,12 +107,12 @@ namespace JXSoft {
} }
public static class TCPClientExtention public static class TCPClientExtention
{ {
public static IUnRegister RegisterMessageEvent<TResponse>(this ITCPClient self, Action<TResponse> onEvent) where TResponse : IResponse public static IUnRegister RegisterMessageEvent<TResponse>(this ITCPClient self, Action<TResponse,string> onEvent) where TResponse : IResponse
{ {
return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => { return TCPMangerArchitecture.Interface.RegisterEvent<ResponseMsgEvent>(e => {
if (e.res.GetType() == typeof(TResponse)) if (e.res.GetType() == typeof(TResponse))
{ {
onEvent.Invoke((TResponse)e.res); onEvent.Invoke((TResponse)e.res,e.sender);
} }
}); });
} }

15
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -28,6 +28,7 @@ namespace JXSoft {
} }
//找到命令后添加命令 //找到命令后添加命令
mCommandContainer.Get<ExcuteResponseCommand<T>>().setProtocolData(protocolData); mCommandContainer.Get<ExcuteResponseCommand<T>>().setProtocolData(protocolData);
mCommandContainer.Get<ExcuteResponseCommand<T>>().setSender(getSender());
this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>()); this.SendCommand(mCommandContainer.Get<ExcuteResponseCommand<T>>());
} }
/// <summary> /// <summary>
@ -81,6 +82,10 @@ namespace JXSoft {
setLinkState((int)LinkStateDefault.Close); setLinkState((int)LinkStateDefault.Close);
return true; return true;
} }
public virtual string getSender() {
return "default";
}
#endregion #endregion
} }
@ -98,6 +103,7 @@ namespace JXSoft {
public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse public class ExcuteResponseCommand<TResponse> : AbstractCommand where TResponse : IResponse
{ {
public string protocolData; public string protocolData;
public string sender;
private TResponse response; private TResponse response;
private Color printColor = Color.white; private Color printColor = Color.white;
public ExcuteResponseCommand(TResponse response) public ExcuteResponseCommand(TResponse response)
@ -115,13 +121,16 @@ namespace JXSoft {
if (response.toProtocolData() != "") { if (response.toProtocolData() != "") {
Debug.Log("Received:" + response.toProtocolData() + response.GetType()); Debug.Log("Received:" + response.toProtocolData() + response.GetType());
} }
this.SendEvent(new ResponseMsgEvent(response)); this.SendEvent(new ResponseMsgEvent(response, sender));
} }
} }
public void setProtocolData(string protocolData) public void setProtocolData(string protocolData)
{ {
this.protocolData = protocolData; this.protocolData = protocolData;
} }
public void setSender(string sender) {
this.sender = sender;
}
} }
/// <summary> /// <summary>
@ -282,9 +291,11 @@ namespace JXSoft {
public struct ResponseMsgEvent public struct ResponseMsgEvent
{ {
public IResponse res; public IResponse res;
public ResponseMsgEvent(IResponse res_) public string sender;
public ResponseMsgEvent(IResponse res_,string sender_)
{ {
res = res_; res = res_;
sender = sender_;
} }
}; };
public struct RequestMsgEvent public struct RequestMsgEvent

Loading…
Cancel
Save