diff --git a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj index 980b340cc95..5a5b816d3d4 100644 --- a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj +++ b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj @@ -69,6 +69,7 @@ + @@ -85,6 +86,10 @@ + + + + diff --git a/src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor b/src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor index 396614dac02..250d040910f 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/SocketFactories.razor @@ -4,6 +4,8 @@

Tcp 套接字服务 ITcpSocketFactory

组件库内置了 Socket 套接字通讯服务

+ +

1. 服务注入

services.AddBootstrapBlazorTcpSocketFactory();
@@ -84,26 +86,26 @@ private async Task CreateClient()

数据适配器设计思路如下

    -
  1. 使用 SocketDataTypeConverterAttribute 标签约定通讯数据使用那个转换类型进行转换 指定类型需继承 ISocketDataConverter +
  2. 使用 DataTypeConverterAttribute 标签约定通讯数据使用那个转换类型进行转换 指定类型需继承 IDataConverter 接口
  3. -
  4. 使用 SocketDataPropertyConverterAttribute 标签约定如何转换数据类型 (Property) 属性值
  5. +
  6. 使用 DataPropertyConverterAttribute 标签约定如何转换数据类型 (Property) 属性值
-
[SocketDataTypeConverter(Type = typeof(SocketDataConverter<MockEntity>))]
+
[DataTypeConverter(Type = typeof(DataConverter<MockEntity>))]
 class MockEntity
 {
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
     public byte[]? Header { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
     public byte[]? Body { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(Foo), Offset = 7, Length = 1, ConverterType = typeof(FooConverter), ConverterParameters = ["test"])]
+    [DataPropertyConverter(Type = typeof(Foo), Offset = 7, Length = 1, ConverterType = typeof(FooConverter), ConverterParameters = ["test"])]
     public string? Value1 { get; set; }
 }
-
class FooConverter(string name) : ISocketDataPropertyConverter
+
class FooConverter(string name) : IDataPropertyConverter
 {
     public object? Convert(ReadOnlyMemory<byte> data)
     {
@@ -114,15 +116,15 @@ class MockEntity
 

针对第三方程序集的数据类型解决方案如下:

使用

-
builder.Services.ConfigureSocketDataConverters(options =>
+
builder.Services.ConfigureDataConverters(options =>
 {
     options.AddTypeConverter<MockEntity>();
-    options.AddPropertyConverter<MockEntity>(entity => entity.Header, new SocketDataPropertyConverterAttribute()
+    options.AddPropertyConverter<MockEntity>(entity => entity.Header, new DataPropertyConverterAttribute()
     {
         Offset = 0,
         Length = 5
     });
-    options.AddPropertyConverter<MockEntity>(entity => entity.Body, new SocketDataPropertyConverterAttribute()
+    options.AddPropertyConverter<MockEntity>(entity => entity.Body, new DataPropertyConverterAttribute()
     {
         Offset = 5,
         Length = 2
diff --git a/src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor b/src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor
index f42a04f24c7..f1498c51879 100644
--- a/src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor
+++ b/src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor
@@ -8,74 +8,74 @@
            Introduction="@Localizer["NormalIntro"]"
            Name="Normal" ShowCode="false">
     

- 通过 SocketDataTypeConverterAttribute 类标签与 SocketDataPropertyConverterAttribute + 通过 DataTypeConverterAttribute 类标签与 DataPropertyConverterAttribute 属性标签可以将通讯数据自动转化为我们系统中需要的业务实体类,示例如下:

-
[SocketDataTypeConverter(Type = typeof(SocketDataConverter<MockEntity>))]
+    
[DataTypeConverter(Type = typeof(DataConverter<MockEntity>))]
 class MockEntity
 {
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
     public byte[]? Header { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
     public byte[]? Body { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(string), Offset = 7, Length = 1, EncodingName = "utf-8")]
+    [DataPropertyConverter(Type = typeof(string), Offset = 7, Length = 1, EncodingName = "utf-8")]
     public string? Value1 { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(int), Offset = 8, Length = 1)]
+    [DataPropertyConverter(Type = typeof(int), Offset = 8, Length = 1)]
     public int Value2 { get; set; }
 }
-

1. SocketDataTypeConverter 参数说明:

+

1. DataTypeConverter 参数说明:

    -
  • Type: 自定义转换器类型,组件库内置了 SocketDataConverter 泛型类,建议看一下源码非常方便扩展出自己的转换器 +
  • Type: 自定义转换器类型,组件库内置了 DataConverter 泛型类,建议看一下源码非常方便扩展出自己的转换器
-

2. SocketDataPropertyConverterAttribute 参数说明

+

2. DataPropertyConverterAttribute 参数说明

  • Type: 转换目标数据类型
  • Offset: 数据偏移量,即在接收到的数据中起始位置
  • Length: 数据长度,即在接收到的数据中占的长度
  • EncodingName: 转成字符串时需要的编码名称,默认 null 使用 utf-8 编码
  • -
  • ConverterType: 自定义转化器类型,继承 ISocketDataPropertyConverter 接口
  • +
  • ConverterType: 自定义转化器类型,继承 IDataPropertyConverter 接口
  • ConverterParameters: 自定义转化器类型构造函数所需的参数,默认 null

组件库内置了大量数据类型转换器

    -
  • SocketDataByteArrayConverter 转成 byte[] 数组类型
  • -
  • SocketDataStringConverter 转成 string 字符串类型
  • -
  • SocketDataEnumConverter 转成 enum 枚举类型
  • -
  • SocketDataBoolConverter 转成 bool 布尔类型
  • -
  • SocketDataInt16BigEndianConverter 转成 short 整形大端读取
  • -
  • SocketDataInt32BigEndianConverter 转成 int 整形大端读取
  • -
  • SocketDataInt64BigEndianConverter 转成 long 长整形大端读取
  • -
  • SocketDataSingleBigEndianConverter 转成 float 单精度浮点数大端读取
  • -
  • SocketDataDoubleBigEndianConverter 转成 double 双精度浮点数大端读取
  • -
  • SocketDataUInt16BigEndianConverter 转成 ushort 无符号整形大端读取
  • -
  • SocketDataUInt32BigEndianConverter 转成 uint 无符号整形大端读取
  • -
  • SocketDataUInt64BigEndianConverter 转成 ulong 无符号长整形大端读取
  • -
  • SocketDataInt16LittleEndianConverter 转成 short 整形小端读取
  • -
  • SocketDataInt32LittleEndianConverter 转成 int 整形小端读取
  • -
  • SocketDataInt64LittleEndianConverter 转成 long 长整形小端读取
  • -
  • SocketDataSingleLittleEndianConverter 转成 float 单精度浮点数小端读取
  • -
  • SocketDataDoubleLittleEndianConverter 转成 double 双精度浮点数小端读取
  • -
  • SocketDataUInt16LittleEndianConverter 转成 ushort 无符号整形小端读取
  • -
  • SocketDataUInt32LittleEndianConverter 转成 uint 无符号整形小端读取
  • -
  • SocketDataUInt64LittleEndianConverter 转成 ulong 无符号整形小端读取
  • +
  • DataByteArrayConverter 转成 byte[] 数组类型
  • +
  • DataStringConverter 转成 string 字符串类型
  • +
  • DataEnumConverter 转成 enum 枚举类型
  • +
  • DataBoolConverter 转成 bool 布尔类型
  • +
  • DataInt16BigEndianConverter 转成 short 整形大端读取
  • +
  • DataInt32BigEndianConverter 转成 int 整形大端读取
  • +
  • DataInt64BigEndianConverter 转成 long 长整形大端读取
  • +
  • DataSingleBigEndianConverter 转成 float 单精度浮点数大端读取
  • +
  • DataDoubleBigEndianConverter 转成 double 双精度浮点数大端读取
  • +
  • DataUInt16BigEndianConverter 转成 ushort 无符号整形大端读取
  • +
  • DataUInt32BigEndianConverter 转成 uint 无符号整形大端读取
  • +
  • DataUInt64BigEndianConverter 转成 ulong 无符号长整形大端读取
  • +
  • DataInt16LittleEndianConverter 转成 short 整形小端读取
  • +
  • DataInt32LittleEndianConverter 转成 int 整形小端读取
  • +
  • DataInt64LittleEndianConverter 转成 long 长整形小端读取
  • +
  • DataSingleLittleEndianConverter 转成 float 单精度浮点数小端读取
  • +
  • DataDoubleLittleEndianConverter 转成 double 双精度浮点数小端读取
  • +
  • DataUInt16LittleEndianConverter 转成 ushort 无符号整形小端读取
  • +
  • DataUInt32LittleEndianConverter 转成 uint 无符号整形小端读取
  • +
  • DataUInt64LittleEndianConverter 转成 ulong 无符号整形小端读取

自定义数据类型转化器示例

-
[SocketTypeDataConverter(Type = typeof(SocketDataConverter<MockEntity>))]
+    
[SocketTypeDataConverter(Type = typeof(DataConverter<MockEntity>))]
 class MockEntity
 {
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 5)]
     public byte[]? Header { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
+    [DataPropertyConverter(Type = typeof(byte[]), Offset = 5, Length = 2)]
     public byte[]? Body { get; set; }
 
-    [SocketDataPropertyConverter(Type = typeof(Foo), Offset = 7, Length = 1, ConverterType = typeof(FooConverter), ConverterParameters = ["test"])]
+    [DataPropertyConverter(Type = typeof(Foo), Offset = 7, Length = 1, ConverterType = typeof(FooConverter), ConverterParameters = ["test"])]
     public string? Value1 { get; set; }
 }
-
class FooConverter(string name) : ISocketDataPropertyConverter
+    
class FooConverter(string name) : IDataPropertyConverter
 {
     public object? Convert(ReadOnlyMemory<byte> data)
     {
diff --git a/src/BootstrapBlazor.Server/Components/Samples/Sockets/Notice.razor b/src/BootstrapBlazor.Server/Components/Samples/Sockets/Notice.razor
index b618855b426..19d8b9e1efc 100644
--- a/src/BootstrapBlazor.Server/Components/Samples/Sockets/Notice.razor
+++ b/src/BootstrapBlazor.Server/Components/Samples/Sockets/Notice.razor
@@ -1,3 +1,5 @@
 
     

ITcpSocketFactory 服务仅在 Server 模式下可用

+ + diff --git a/src/BootstrapBlazor.Server/Locales/en-US.json b/src/BootstrapBlazor.Server/Locales/en-US.json index 57bc7a42645..3f1357895ae 100644 --- a/src/BootstrapBlazor.Server/Locales/en-US.json +++ b/src/BootstrapBlazor.Server/Locales/en-US.json @@ -7129,7 +7129,7 @@ "DataEntityTitle": "Socket Auto Entity", "DataEntityDescription": "After receiving the communication data, it is automatically converted into the entity class required by the business", "NormalTitle": "Basic usage", - "NormalIntro": "Enable automatic data conversion through the SocketDataTypeConverterAttribute attribute" + "NormalIntro": "Enable automatic data conversion through the DataTypeConverterAttribute attribute" }, "BootstrapBlazor.Server.Components.Samples.NetworkMonitors": { "NetworkMonitorTitle": "NetworkMonitor", diff --git a/src/BootstrapBlazor.Server/Locales/zh-CN.json b/src/BootstrapBlazor.Server/Locales/zh-CN.json index c4a0301e360..4844165f379 100644 --- a/src/BootstrapBlazor.Server/Locales/zh-CN.json +++ b/src/BootstrapBlazor.Server/Locales/zh-CN.json @@ -7129,7 +7129,7 @@ "DataEntityTitle": "Socket 数据转化实体类", "DataEntityDescription": "接收到通讯数据后自动转成业务需要的实体类", "NormalTitle": "基本用法", - "NormalIntro": "通过 SocketDataTypeConverterAttribute 标签开启数据自动转换功能" + "NormalIntro": "通过 DataTypeConverterAttribute 标签开启数据自动转换功能" }, "BootstrapBlazor.Server.Components.Samples.NetworkMonitors": { "NetworkMonitorTitle": "NetworkMonitor 网络状态",