Skip to content

Commit 219008b

Browse files
committed
doc: 增加数据适配器文档
1 parent ad932d8 commit 219008b

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@page "/socket/adapter"
2+
@inject IStringLocalizer<Adapters> Localizer
3+
4+
<h3>@Localizer["AdaptersTitle"]</h3>
5+
<h4>@Localizer["AdaptersDescription"]</h4>
6+
7+
<Notice></Notice>
8+
9+
<DemoBlock Title="@Localizer["NormalTitle"]"
10+
Introduction="@Localizer["NormalIntro"]"
11+
Name="Normal" ShowCode="false">
12+
<p>本例中连接一个模拟自定义协议服务,每次接收到客户端发来的特定数据后,返回业务数据。这类应用在我们实际应用中非常常见</p>
13+
<p>通过 <code>SocketClientOptions</code> 配置类关闭自动接收数据功能 <code>IsAutoReceive="false"</code></p>
14+
<Pre>_client = TcpSocketFactory.GetOrCreate("demo-adapter", options =>
15+
{
16+
options.IsAutoReceive = false;
17+
options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
18+
});</Pre>
19+
<ul class="ul-demo">
20+
<li>点击 <b>连接</b> 按钮后通过 <code>ITcpSocketFactory</code> 服务实例创建的 <code>ITcpSocketClient</code> 对象连接到网站模拟 <code>TcpServer</code></li>
21+
<li>点击 <b>断开</b> 按钮调用 <code>CloseAsync</code> 方法断开 Socket 连接</li>
22+
<li>点击 <b>发送</b> 按钮调用 <code>SendAsync</code> 方法发送请求数据</li>
23+
</ul>
24+
<p>本例中已关闭自动接收功能,调用 <code>ReceivedAsync</code> 方法主动接收服务端返回数据</p>
25+
26+
<div class="row form-inline g-3">
27+
<div class="col-12 col-sm-6">
28+
<Button Text="连接" Icon="fa-solid fa-play"
29+
OnClick="OnConnectAsync" IsDisabled="@_client.IsConnected"></Button>
30+
<Button Text="断开" Icon="fa-solid fa-stop" class="ms-2"
31+
OnClick="OnCloseAsync" IsDisabled="@(!_client.IsConnected)"></Button>
32+
<Button Text="发送" Icon="fa-solid fa-paper-plane" class="ms-2" IsAsync="true"
33+
OnClick="OnSendAsync" IsDisabled="@(!_client.IsConnected)"></Button>
34+
</div>
35+
<div class="col-12">
36+
<Console Items="@_items" Height="496" HeaderText="接收数据(间隔 10 秒)"
37+
ShowAutoScroll="true" OnClear="@OnClear"></Console>
38+
</div>
39+
</div>
40+
</DemoBlock>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Net;
7+
8+
namespace BootstrapBlazor.Server.Components.Samples.Sockets;
9+
10+
/// <summary>
11+
/// 数据适配器示例
12+
/// </summary>
13+
public partial class Adapters : IDisposable
14+
{
15+
[Inject, NotNull]
16+
private ITcpSocketFactory? TcpSocketFactory { get; set; }
17+
18+
private ITcpSocketClient _client = null!;
19+
20+
private List<ConsoleMessageItem> _items = [];
21+
22+
private readonly IPEndPoint _serverEndPoint = new(IPAddress.Loopback, 8900);
23+
24+
private CancellationTokenSource _connectTokenSource = new();
25+
private CancellationTokenSource _sendTokenSource = new();
26+
27+
/// <summary>
28+
/// <inheritdoc/>
29+
/// </summary>
30+
protected override void OnInitialized()
31+
{
32+
base.OnInitialized();
33+
34+
// 从服务中获取 Socket 实例
35+
_client = TcpSocketFactory.GetOrCreate("demo-adapter", options =>
36+
{
37+
options.IsAutoReceive = false;
38+
options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
39+
});
40+
}
41+
42+
private async Task OnConnectAsync()
43+
{
44+
if (_client is { IsConnected: false })
45+
{
46+
await _client.ConnectAsync(_serverEndPoint, _connectTokenSource.Token);
47+
}
48+
}
49+
50+
private async Task SendAsync()
51+
{
52+
if (_client is { IsConnected: false })
53+
{
54+
// 准备通讯数据
55+
var data = new byte[1024];
56+
await _client.SendAsync(data, _sendTokenSource.Token);
57+
}
58+
}
59+
60+
private async Task OnCloseAsync()
61+
{
62+
if (_client is { IsConnected: true })
63+
{
64+
await _client.CloseAsync();
65+
}
66+
}
67+
68+
private Task OnClear()
69+
{
70+
_items = [];
71+
return Task.CompletedTask;
72+
}
73+
74+
private async ValueTask OnReceivedAsync(ReadOnlyMemory<byte> data)
75+
{
76+
// 将数据显示为十六进制字符串
77+
var payload = System.Text.Encoding.UTF8.GetString(data.Span);
78+
_items.Add(new ConsoleMessageItem
79+
{
80+
Message = $"接收到来自站点的数据为 {payload}"
81+
});
82+
83+
// 保持队列中最大数量为 50
84+
if (_items.Count > 50)
85+
{
86+
_items.RemoveAt(0);
87+
}
88+
await InvokeAsync(StateHasChanged);
89+
}
90+
91+
private void Dispose(bool disposing)
92+
{
93+
if (disposing)
94+
{
95+
if (_client is { IsConnected: true })
96+
{
97+
98+
}
99+
}
100+
}
101+
102+
/// <summary>
103+
/// <inheritdoc/>
104+
/// </summary>
105+
public void Dispose()
106+
{
107+
Dispose(true);
108+
GC.SuppressFinalize(this);
109+
}
110+
}

0 commit comments

Comments
 (0)