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.
65 lines
1.7 KiB
65 lines
1.7 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using JXSoft; |
|
|
|
public class TCPClientExample : MonoBehaviour,ITCPClient |
|
{ |
|
// Start is called before the first frame update |
|
void Start() |
|
{ |
|
//注册接收到数据类型事件监听 |
|
this.RegisterMessageEvent<YourResponse>((e,sender) => { |
|
Debug.Log("TestGetMsg:"+e.msg); |
|
Debug.Log("IpAddress:"+sender); |
|
//应答 |
|
if (sender == "127.0.0.1:20000") { |
|
this.sendRequest<StringRequest, TCPClientModel>(new StringRequest("Received!")); |
|
} |
|
if (sender == "TCP2号机") { |
|
this.sendRequest<StringRequest, TCPClient2Model>(new StringRequest("Received2!")); |
|
} |
|
//取消接收该数据 |
|
//this.offReceive<YourResponse>(); |
|
}); |
|
//开启开数据类型接收 |
|
this.onReceive<YourResponse>(); |
|
this.onReceive<YourResponse,TCPClient2Model>(); |
|
} |
|
} |
|
#region ResReqData |
|
public class YourResponse : AbstractJsonResponse |
|
{ |
|
public string msg; |
|
public YourResponse() |
|
{ |
|
this.msg = ""; |
|
} |
|
} |
|
public struct YourNumberFilterResponse : IResponse |
|
{ |
|
public int codeNum; |
|
private string exceptionMsg; |
|
public string toProtocolData() |
|
{ |
|
return "你想要打印的调试信息,无需打印返回空字符串即可"; |
|
} |
|
|
|
public bool trySetData(string protocolData) |
|
{ |
|
bool isNumber = int.TryParse(protocolData, out codeNum); |
|
if (!isNumber) |
|
{ |
|
exceptionMsg = "这不是数字"; |
|
} |
|
return isNumber; |
|
} |
|
} |
|
|
|
public class YourRequest:IRequest { |
|
public string toProtocolData() |
|
{ |
|
return "要发的请求内容"; |
|
} |
|
} |
|
#endregion
|
|
|