NexTcpSocket 2.1.2
dotnet add package NexTcpSocket --version 2.1.2
NuGet\Install-Package NexTcpSocket -Version 2.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NexTcpSocket" Version="2.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NexTcpSocket" Version="2.1.2" />
<PackageReference Include="NexTcpSocket" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NexTcpSocket --version 2.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NexTcpSocket, 2.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NexTcpSocket@2.1.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NexTcpSocket&version=2.1.2
#tool nuget:?package=NexTcpSocket&version=2.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NexTCPSocket
ABOUT
用于向 Nrc 系列机器人控制器收发数据的.NET 库。
.NET 8 / C# / DotNetty
推荐使用 Nuget 安装库并使用https://www.nuget.org/packages/NexTCPSocket。
编译
生成项目-生成 NexTCPSocket
API
Client
这是客户端的基础类
构造函数
Client(NexTCPHandler handler);
属性
属性名 | 类型 | 描述 | 读/写 |
---|---|---|---|
Host | String | 连接到的主机名,IP:Port | 只读 |
Handler | NexTCPHandler | TCP 连接处理器,需要继承 NexTCPHandler 抽象类 | 只读 |
方法
方法 | 描述 |
---|---|
void Connect(string ip, int port) | 连接到控制器,使用 IP 和端口 |
void Connect(string host) | 连接到控制器,host:"ip:port" |
async Task SendMessage(NexTCPMessage message) | 发送数据 |
async Task SendMessage(int command, string data) | 发送数据 |
async Task SendMessage(int command, byte[] data) | 发送数据 |
async Task SendMessage(int command, object data) | 发送数据 |
async Task SendMessage(NexTCPSocket.Command command, object data) | 发送数据 |
async Task<T> SendMessage<T>(int command, object data, int responseCommand, Func<T, bool> predicate = null, int timeout = 3000) | 发送数据,并等待返回数据 |
async Task<T> SendMessage<T>(Command command, object data, Command responseCommand, Func<T, bool> predicate = null, int timeout = 3000) | 发送数据,并等待返回数据 |
void RegisterTCPHandler(int command, Action<NexTCPMessage> CallBack) | 注册一个新的自定义 TCP 数据处理器 |
void RegisterTCPHandler(Command command, Action<NexTCPMessage> CallBack) | 注册一个新的自定义 TCP 数据处理器 |
void RemoveTCPHandler(int command, Action<NexTCPSocket.NexTCPMessage> CallBack) | 删除一个自定义 TCP 数据处理器 |
void DisConnect() | 关闭连接 |
public void Dispose() | 释放资源 |
事件
事件 | 描述 |
---|---|
MessageSend | 发送数据事件,参数为数据内容 |
ConnectError | 连接错误事件,参数为错误内容 |
BeginConnect | 开始连接事件,参数为 IP:Port |
ConnectComplete | 连接确定完成事件,参数为 IP:Port |
ConnectFinished | 连接断开事件,参数为 IP:Port |
NexTCPHandler
抽象类,TCP 连接处理器,包含连接状态回调、接收消息回调。用户需自行继承并实现。
属性
属性 | 类型 | 描述 | 读/写 |
---|---|---|---|
TCPClient | NexTCPSocket.Client | 客户端实例引用 | 只读 |
State | bool | 是否已连接 | 只读 |
IP | String | IP 地址 | 只读 |
Port | int | 端口号 | 只读 |
方法
方法 | 描述 |
---|---|
virtual void Handler(NexTCPMessage message) | 接收到消息的回调 |
virtual void ConnectState(bool state) | 连接状态改变回调 |
virtual void ConnectCompleteHandler() | 连接确定成功后的回调,如果需要在连接到控制器后发送数据,请放在这里,也可以使用 Client 的 ConnectComplete 事件 |
NexTCPMessage
控制器接收数据实体类
属性 | 类型 | 描述 | 读/写 |
---|---|---|---|
Data | String | 接收到的数据 Data 字段,可以用 JSON 反序列化 | 读写 |
Command | int | 命令字 | 读写 |
Command
命令字枚举
使用
using NexTCPSocket;
namespace YourProject
{
public class MyTCPHandler:NexTCPHandler
{
//以下ConnectCompleteHandler、ConnectState两个方法可不重写,使用Client的事件效果一样。
public override void ConnectCompleteHandler()
{
//连接成功后的初始化数据
//xxxxx
base.ConnectCompleteHandler();
}
public override void ConnectState(bool state)
{
if(state){
Logger.Debug($"连接成功");
}else{
Logger.Debug($"连接断开");
}
base.ConnectState(state);
}
public override void Handler(NexTCPSocket.NexTCPMessage message)
{
Logger.Debug($"TCP接收到数据{message.Command.ToString("X")},{message.Data}");
base.Handler(message);
}
}
public partial class MainWindow
{
NexTCPSocket.Client client = new NexTCPSocket.Client(new MyTCPHandler());
public MainWindow()
{
InitializeComponent();
System.Diagnostics.Trace.WriteLine("软件打开");
BeginConnect += TCPClient_BeginConnect;
ConnectError += TCPClient_ConnectError;
ConnectFinished += TCPClient_ConnectFinished;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
client.Connect("ip地址", 6000);
}
private async void Button1_Click_1(object sender, RoutedEventArgs e)
{
client.SendMessage(0x5565,"sth");
ResultMessage result = await client.SendMessage<ResultMessage>(0x5555,new RequireMessage(),0x5566);
}
private void TCPClient_ConnectFinished(object sender, string host)
{
Logger.Info($"连接结束{host}");
}
private void TCPClient_ConnectError(object sender, string e)
{
Logger.Error($"连接错误{e}");
}
private void TCPClient_BeginConnect(object sender, string host)
{
Logger.Info($"开始连接{host}");
}
protected override void OnClosing(CancelEventArgs e)
{
client.DisConnect();
base.OnClosing(e);
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- DotNetty.Buffers (>= 0.7.6)
- DotNetty.Codecs (>= 0.7.6)
- DotNetty.Common (>= 0.7.6)
- DotNetty.Handlers (>= 0.7.6)
- DotNetty.Transport (>= 0.7.6)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.