Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<PackageReference Include="BootstrapBlazor.SummerNote" Version="9.0.7" />
<PackageReference Include="BootstrapBlazor.TableExport" Version="9.2.6" />
<PackageReference Include="BootstrapBlazor.Tasks.Dashboard" Version="9.0.0" />
<PackageReference Include="BootstrapBlazor.TcpSocket" Version="9.0.0" />
<PackageReference Include="BootstrapBlazor.TcpSocket" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.Topology" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.UniverIcon" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.UniverSheet" Version="9.0.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@
<li>不使用 <b>数据适配器</b> 要分两次接收才能接收完整</li>
<li>使用 <b>数据适配器</b> 一次即可接收完整数据包</li>
</ul>
<Pre>private readonly DataPackageAdapter _dataAdapter = new()
{
// 数据适配器内部使用固定长度数据处理器
DataPackageHandler = new FixLengthDataPackageHandler(12)
};
<Pre>// 数据适配器内部使用固定长度数据处理器
private readonly DataPackageAdapter _dataAdapter = new(new FixLengthDataPackageHandler(12));

_dataAdapter.ReceivedCallBack += async Data =>
_dataAdapter.ReceivedCallBack = async Data =>
{
// 此处接收到的数据 Data 为完整响应数据
};</Pre>
<Pre>// 实战中可以使用 ITcpSocketClient 扩展方法 SetDataPackageAdapter 简化代码
_client.SetDataPackageAdapter(_dataAdapter, UpdateReceiveLog);</Pre>

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

<div class="row form-inline g-3">
<div class="col-12">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public partial class Adapters : IDisposable
private readonly CancellationTokenSource _connectTokenSource = new();
private readonly CancellationTokenSource _sendTokenSource = new();
private readonly CancellationTokenSource _receiveTokenSource = new();
private readonly DataPackageAdapter _dataAdapter = new()
{
DataPackageHandler = new FixLengthDataPackageHandler(12)
};
private readonly DataPackageAdapter _dataAdapter = new(new FixLengthDataPackageHandler(12));
private bool _useDataAdapter = true;

/// <summary>
Expand All @@ -46,13 +43,14 @@ protected override void OnInitialized()
// 设置本地使用的 IP地址与端口
options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
});

// 此处代码分开写是为了判断 _useDataAdapter 参数
_client.ReceivedCallBack += OnReceivedAsync;
_dataAdapter.ReceivedCallBack = UpdateReceiveLog;

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

_dataAdapter.ReceivedCallBack += async data =>
{
// 直接处理接收的数据
await UpdateReceiveLog(data);
};
}

private async Task OnConnectAsync()
Expand Down Expand Up @@ -102,7 +100,7 @@ private async ValueTask OnReceivedAsync(ReadOnlyMemory<byte> data)
}
}

private async Task UpdateReceiveLog(ReadOnlyMemory<byte> data)
private async ValueTask UpdateReceiveLog(ReadOnlyMemory<byte> data)
{
var payload = Encoding.UTF8.GetString(data.Span);
var body = BitConverter.ToString(data.ToArray());
Expand Down
Loading