Jeff.SocketHelper
0.1.6
dotnet add package Jeff.SocketHelper --version 0.1.6
NuGet\Install-Package Jeff.SocketHelper -Version 0.1.6
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="Jeff.SocketHelper" Version="0.1.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jeff.SocketHelper" Version="0.1.6" />
<PackageReference Include="Jeff.SocketHelper" />
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 Jeff.SocketHelper --version 0.1.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Jeff.SocketHelper, 0.1.6"
#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 Jeff.SocketHelper@0.1.6
#: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=Jeff.SocketHelper&version=0.1.6
#tool nuget:?package=Jeff.SocketHelper&version=0.1.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
概述
本类库封装了 Tcp Server 的简单事件处理方法, 便于开发者直接处理业务。
快速开始
builder.Services.AddJeffSocket(opt =>
{
//注册一个 Tcp Server, 监听 [本机ip:19527]
opt.AddTcpServerForIPv4<MyTcpServerHandler>(null, 19527, cfg =>
{
cfg.ReceiveTimeout = 60;
cfg.SendTimeout = 60;
});
});
/// <summary>
/// 创建一个 ITcpServerHandler 的实现类, 用于处理 Tcp Server 相关事件。
/// ITcpServerHandler 的实现类自动注册为 Scoped 服务。
/// </summary>
public class MyTcpServerHandler : ITcpServerHandler
{
private ILogger<MyTcpServerHandler> _logger;
public MyTcpServerHandler(ILogger<MyTcpServerHandler> logger)
{
_logger = logger;
}
/// <summary>
/// 创建 Tcp Server, 开始监听
/// </summary>
public void OnStart(IPEndPoint serverPoint)
{
Console.WriteLine($@"新的 Tcp Server 开始监听, 地址: {serverPoint}");
_logger.LogInformation(@"新的 Tcp Server 开始监听, 地址: {serverPoint}", serverPoint);
}
/// <summary>
/// 连接远程客户端时触发
/// </summary>
public void OnConnect(IPEndPoint? remotePoint, long connectionIndex)
{
Console.WriteLine($@"开始一个新的连接: {connectionIndex}");
}
/// <summary>
/// 断开远程客户端时触发
/// </summary>
public void OnDisconnect(IPEndPoint? remotePoint, string reason, long connectionIndex)
{
Console.WriteLine($@"一个连接断开, 连接索引:{connectionIndex}, 断开原因:{reason}");
}
/// <summary>
/// 接受数据时触发
/// </summary>
public void OnReceive(IPEndPoint? remotePoint, byte[] receivedBytes, ref byte[] sendBytes, long connectionIndex, long dataIndex)
{
string received = Encoding.UTF8.GetString(receivedBytes);
Console.WriteLine($@"收到一条数据, 连接索引:{connectionIndex}, 数据索引:{dataIndex}, 数据:{received}");
//回发信息
sendBytes = Encoding.UTF8.GetBytes($@"你好, 我已收到数据:{received}");
}
}
| 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
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- System.Net.Sockets (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.