Skip to content

Commit 69339a5

Browse files
authored
doc(ITcpSocketClient): update AddDataPackageAdapter documentation (#6686)
* doc: 增加配置示例文档 * doc: 增加本地化资源文件 * doc: 更新示例 * doc: 更新数据适配器文档 * chore: 更新依赖包 * doc: 更新文档
1 parent 481b744 commit 69339a5

File tree

7 files changed

+57
-8
lines changed

7 files changed

+57
-8
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@
6868
<PackageReference Include="BootstrapBlazor.RDKit" Version="9.0.2" />
6969
<PackageReference Include="BootstrapBlazor.SignaturePad" Version="9.0.1" />
7070
<PackageReference Include="BootstrapBlazor.SmilesDrawer" Version="9.0.2" />
71+
<PackageReference Include="BootstrapBlazor.Socket" Version="9.0.4" />
7172
<PackageReference Include="BootstrapBlazor.Sortable" Version="9.0.3" />
7273
<PackageReference Include="BootstrapBlazor.Splitting" Version="9.0.3" />
7374
<PackageReference Include="BootstrapBlazor.SvgEditor" Version="9.0.3" />
7475
<PackageReference Include="BootstrapBlazor.SummerNote" Version="9.0.7" />
7576
<PackageReference Include="BootstrapBlazor.TableExport" Version="9.2.6" />
7677
<PackageReference Include="BootstrapBlazor.Tasks.Dashboard" Version="9.0.0" />
77-
<PackageReference Include="BootstrapBlazor.TcpSocket" Version="9.0.1" />
78+
<PackageReference Include="BootstrapBlazor.TcpSocket" Version="9.0.4" />
7879
<PackageReference Include="BootstrapBlazor.Topology" Version="9.0.1" />
7980
<PackageReference Include="BootstrapBlazor.UniverIcon" Version="9.0.1" />
8081
<PackageReference Include="BootstrapBlazor.UniverSheet" Version="9.0.5" />

src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ private async Task CreateClient()
6161
{
6262
DataPackageHandler = new FixLengthDataPackageHandler(4)
6363
};
64-
client.SetDataPackageAdapter(adapter, buffer =>
64+
65+
// 如果 client 不销毁切记使用 RemoveDataPackageAdapter 移除回调委托防止内存泄露
66+
client.AddDataPackageAdapter(adapter, buffer =>
6567
{
6668
// buffer 即是接收到的数据
6769
return ValueTask.CompletedTask;

src/BootstrapBlazor.Server/Components/Samples/Sockets/Adapters.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ _dataAdapter.ReceivedCallBack = async Data =>
5151
{
5252
// 此处接收到的数据 Data 为完整响应数据
5353
};</Pre>
54-
<Pre>// 实战中可以使用 ITcpSocketClient 扩展方法 SetDataPackageAdapter 简化代码
55-
_client.SetDataPackageAdapter(_dataAdapter, UpdateReceiveLog);</Pre>
54+
<Pre>// 实战中可以使用 ITcpSocketClient 扩展方法 AddDataPackageAdapter 简化代码
55+
// 如果 _client 实例在当前页面不销毁时,切记使用 AddDataPackageAdapter 移除当前页面加载的 UpdateReceiveLog 回调方法避免内存泄露
56+
_client.AddDataPackageAdapter(_dataAdapter, UpdateReceiveLog);</Pre>
5657

5758
<p>本例中使用的模拟服务端代码如下:</p>
5859

src/BootstrapBlazor.Server/Components/Samples/Sockets/Adapters.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override void OnInitialized()
4949
_dataAdapter.ReceivedCallBack = UpdateReceiveLog;
5050

5151
// 实战中可以通过下面一句话设置数据适配器与回调方法
52-
// _client.SetDataPackageAdapter(_dataAdapter, UpdateReceiveLog);
52+
// _client.AddDataPackageAdapter(_dataAdapter, UpdateReceiveLog);
5353

5454
}
5555

src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MockEntity
6363
<li><code>DataUInt64LittleEndianConverter</code> 转成 ulong 无符号整形小端读取</li>
6464
</ul>
6565
<p>自定义数据类型转化器示例</p>
66-
<Pre>[SocketTypeDataConverter(Type = typeof(DataConverter&lt;MockEntity&gt;))]
66+
<Pre>[DataTypeConverter(Type = typeof(DataConverter&lt;MockEntity&gt;))]
6767
class MockEntity
6868
{
6969
[DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
@@ -84,3 +84,44 @@ class MockEntity
8484
}</Pre>
8585
<p>更多技术细节可以参考以上内置提供的转换器源码</p>
8686
</DemoBlock>
87+
88+
<DemoBlock Title="@Localizer["ConfigTitle"]"
89+
Introduction="@Localizer["ConfigIntro"]"
90+
Name="Config" ShowCode="false">
91+
<p>通过 <code>ConfigureDataConverters</code> 可以配置任意类型(第三方程序集中无源码类型)自定数据转换</p>
92+
<Pre>class MockEntity
93+
{
94+
public byte[]? Header { get; set; }
95+
96+
public byte[]? Body { get; set; }
97+
}</Pre>
98+
<Pre>service.ConfigureDataConverters(options =>
99+
{
100+
// 配置 MockEntity 转换
101+
options.AddTypeConverter&lt;MockEntity&gt;();
102+
103+
// 配置 MockEntity 属性 Header 转换逻辑 从起始位置 0 开始读取 5 个字节
104+
options.AddPropertyConverter&lt;MockEntity&gt;(entity => entity.Header, new DataPropertyConverterAttribute()
105+
{
106+
Offset = 0,
107+
Length = 5
108+
});
109+
110+
// 配置 MockEntity 属性 Body 转换逻辑 从起始位置 5 开始读取 2 个字节
111+
options.AddPropertyConverter&lt;MockEntity&gt;(entity => entity.Body, new DataPropertyConverterAttribute()
112+
{
113+
Offset = 5,
114+
Length = 2
115+
});
116+
});</Pre>
117+
<p>通过调用 <code>ITcpSocketClient</code> 实例泛型扩展方法 <code>AddDataPackageAdapter</code> 在回调方法 <code>OnReceiveAsync</code> 中直接拿到数据实体类</p>
118+
<Pre>// 创建一个数据适配器
119+
var adapter = new DataPackageAdapter(new FixLengthDataPackageHandler(7));
120+
121+
// 设置数据适配器与回调方法
122+
_client.AddDataPackageAdapter&lt;MockEntity&gt;(adapter, OnReceiveAsync);</Pre>
123+
<Pre>private async Task OnReceiveAsync(MockEntity? entity)
124+
{
125+
return Task.CompletedTask;
126+
}</Pre>
127+
</DemoBlock>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7163,7 +7163,9 @@
71637163
"DataEntityTitle": "Socket Auto Entity",
71647164
"DataEntityDescription": "After receiving the communication data, it is automatically converted into the entity class required by the business",
71657165
"NormalTitle": "Basic usage",
7166-
"NormalIntro": "Enable automatic data conversion through the <code>DataTypeConverterAttribute</code> attribute"
7166+
"NormalIntro": "Enable automatic data conversion through the <code>DataTypeConverterAttribute</code> attribute",
7167+
"ConfigTitle": "DataType Converter",
7168+
"ConfigIntro": "Use the <code>ConfigureDataConverters</code> method to map the entity class and automatically convert the received data"
71677169
},
71687170
"BootstrapBlazor.Server.Components.Samples.NetworkMonitors": {
71697171
"NetworkMonitorTitle": "NetworkMonitor",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7165,7 +7165,9 @@
71657165
"DataEntityTitle": "Socket 数据转化实体类",
71667166
"DataEntityDescription": "接收到通讯数据后自动转成业务需要的实体类",
71677167
"NormalTitle": "基本用法",
7168-
"NormalIntro": "通过 <code>DataTypeConverterAttribute</code> 标签开启数据自动转换功能"
7168+
"NormalIntro": "通过 <code>DataTypeConverterAttribute</code> 标签开启数据自动转换功能",
7169+
"ConfigTitle": "数据实体类映射配置",
7170+
"ConfigIntro": "使用 <code>ConfigureDataConverters</code> 方法对实体类进行映射配置将接收到的数据自动转化"
71697171
},
71707172
"BootstrapBlazor.Server.Components.Samples.NetworkMonitors": {
71717173
"NetworkMonitorTitle": "NetworkMonitor 网络状态",

0 commit comments

Comments
 (0)