You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
|
|
|
namespace NDSD_Screwdriver.Tool
|
|
|
|
|
{
|
|
|
|
|
public class ClientFactory
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TcpClient client = new TcpClient();
|
|
|
|
|
|
|
|
|
|
private IWaitingClient<ITcpClient, IReceiverResult> waitClient;
|
|
|
|
|
|
|
|
|
|
public ClientFactory(string ip)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
client.ConnectAsync(ip).ConfigureAwait(false).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
//调用CreateWaitingClient获取到IWaitingClient的对象。
|
|
|
|
|
waitClient = client.CreateWaitingClient(new WaitingOptions()
|
|
|
|
|
{
|
|
|
|
|
FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
|
|
|
|
|
true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] Send(string str)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//然后使用SendThenReturn。
|
|
|
|
|
byte[] returnData = waitClient.SendThenReturn(Encoding.UTF8.GetBytes(str));
|
|
|
|
|
Console.WriteLine($"收到回应消息:{Encoding.UTF8.GetString(returnData)}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
|
|
|
|
|
|
//同时,如果适配器收到数据后,返回的并不是字节,而是IRequestInfo对象时,可以使用SendThenResponse.
|
|
|
|
|
//ResponsedData responsedData = await waitClient.SendThenResponse(Encoding.UTF8.GetBytes("RRQM"));
|
|
|
|
|
//IRequestInfo requestInfo = responsedData.RequestInfo;//同步收到的RequestInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|