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.

46 lines
1.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
}
}
}