Skip to content

Commit e3971e7

Browse files
authored
doc(ITcpSocketFactory): add ITcpSocketFactory documentation (#6256)
* doc: 增加 Socket 示例 * chore: 增加 Socket 服务菜单 * doc: 增加源码映射文件
1 parent 07d6375 commit e3971e7

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
@page "/socket-factory"
2+
@inject IStringLocalizer<SocketFactories> Localizer
3+
4+
<h3>Tcp 套接字服务 <code>ITcpSocketFactory</code></h3>
5+
<h4>组件库内置了 Socket 套接字通讯服务</h4>
6+
7+
<p class="code-label">1. 服务注入</p>
8+
9+
<Pre>[Inject]
10+
[NotNull]
11+
private ITcpSocketFactory? TcpSocketFactory { get; set; }</Pre>
12+
13+
<p class="code-label">2. 使用服务</p>
14+
<p>调用 <code>TcpSocketFactory</code> 实例方法 <code>GetOrCreate</code> 即可得到一个 <code>ITcpSocketClient</code> 实例。内部提供复用机制,调用两次得到的 <code>ITcpSocketClient</code> 为同一对象</p>
15+
16+
<Pre>var client = factory.GetOrCreate("192.168.1.100", 0);</Pre>
17+
18+
<p class="code-label">3. 使用方法</p>
19+
20+
<ul class="ul-demo">
21+
<li>通过 <code>ITcpSocketClient</code> 实例方法 <code>ConnectAsync</code> 连接远端节点</li>
22+
<li>通过 <code>ITcpSocketClient</code> 实例方法 <code>SendAsync</code> 发送协议数据</li>
23+
<li>通过 <code>ITcpSocketClient</code> 实例方法 <code>Close</code> 关闭连接</li>
24+
<li>通过 <code>ITcpSocketClient</code> 实例方法 <code>SetDataHandler</code> 方法设置数据处理器</li>
25+
</ul>
26+
27+
<p class="code-label">4. 数据处理器</p>
28+
29+
<p>在我们实际应用中,建立套接字连接后就会进行数据通信,数据通信不会是杂乱无章的随机数据,在应用中都是有双方遵守的规约简称通讯协议,在通讯协议的约束下,发送方与接收方均根据通讯协议进行编码或解码工作,将数据有条不紊的传输</p>
30+
31+
<p>数据处理器设计初衷就是为了契合通讯协议大大简化我们开发逻辑,我们已通讯协议每次通讯电文均为 <b>4</b> 位定长举例说明,在实际的通讯过程中,我们接收到的通讯数据存在粘包或者分包的现象</p>
32+
33+
<ul class="ul-demo">
34+
<li>粘包:比如我们期望收到 <b>1234</b> 四个字符,实际上我们接收到的是 <b>123412</b> 多出来的 <b>12</b> 其实是下一个通讯电文的内容,相邻两个通讯数据包的粘连称为粘包</li>
35+
<li>分包:比如我们期望收到 <b>1234</b> 四个字符,实际上我们接收到的是 <b>12</b> 和 <b>34</b> 两个数据包,这种情况称为分包</li>
36+
</ul>
37+
38+
<p>我们内置了 <code>IDataPackageHandler</code> 数据包处理接口,已经虚类 <code>DataPackageHandlerBase</code> 作为数据处理器基类已经内置了 <b>粘包</b> <b>分包</b> 的逻辑,继承此类后专注自己处理的业务即可</p>
39+
40+
<p>此外我们还内置了 <code>FixLengthDataPackageHandler</code> <b>固定长度数据处理器</b> 使用方法如下:</p>
41+
42+
<Pre>[Inject]
43+
[NotNull]
44+
private ITcpSocketFactory? TcpSocketFactory { get; set; }
45+
46+
private async Task CreateClient()
47+
{
48+
// 创建 ITcpSocketClient 实例
49+
var client = TcpSocketFactory.GetOrCreate("localhost", 0);
50+
51+
// 设置 FixLengthDataPackageHandler 数据处理器处理数据定长 4 的数据
52+
client.SetDataHandler(new FixLengthDataPackageHandler(4)
53+
{
54+
// 设置接收数据回调方法
55+
ReceivedCallBack = buffer =>
56+
{
57+
// 内部自动处理粘包分包问题,这里参数 buffer 一定是定长为 4 的业务数据
58+
receivedBuffer = buffer;
59+
return Task.CompletedTask;
60+
}
61+
});
62+
63+
// 连接远端节点 连接成功后自动开始接收数据
64+
var connected = await client.ConnectAsync("192.168.10.100", 6688);
65+
}
66+
</Pre>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
namespace BootstrapBlazor.Server.Components.Samples;
7+
8+
/// <summary>
9+
/// ISocketFactory 服务说明文档
10+
/// </summary>
11+
public partial class SocketFactories
12+
{
13+
14+
}

src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,11 @@ void AddServices(DemoMenuItem item)
15771577
Url = "print-service"
15781578
},
15791579
new()
1580+
{
1581+
Text = Localizer["TcpSocketFactory"],
1582+
Url = "socket-factory"
1583+
},
1584+
new()
15801585
{
15811586
Text = Localizer["ThemeProvider"],
15821587
Url = "theme-provider"

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4951,7 +4951,8 @@
49514951
"AvatarUpload": "AvatarUpload",
49524952
"CardUpload": "CardUpload",
49534953
"DropUpload": "DropUpload",
4954-
"Vditor": "Vditor Markdown"
4954+
"Vditor": "Vditor Markdown",
4955+
"TcpSocketFactory": "ITcpSocketFactory"
49554956
},
49564957
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
49574958
"TablesHeaderTitle": "Header grouping function",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4951,7 +4951,8 @@
49514951
"AvatarUpload": "头像上传组件 AvatarUpload",
49524952
"CardUpload": "卡片上传组件 CardUpload",
49534953
"DropUpload": "拖动上传组件 DropUpload",
4954-
"Vditor": "富文本框 Vditor Markdown"
4954+
"Vditor": "富文本框 Vditor Markdown",
4955+
"TcpSocketFactory": "TCP Socket 套接字服务 ITcpSocketFactory"
49554956
},
49564957
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
49574958
"TablesHeaderTitle": "表头分组功能",

src/BootstrapBlazor.Server/docs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@
240240
"audio-device": "AudioDevices",
241241
"fullscreen-button": "FullScreenButtons",
242242
"meet": "Meets",
243-
"vditor": "Vditors"
243+
"vditor": "Vditors",
244+
"socket-factory": "SocketFactories"
244245
},
245246
"video": {
246247
"table": "BV1ap4y1x7Qn?p=1",

0 commit comments

Comments
 (0)