Skip to content

Commit 959d8c2

Browse files
committed
2 parents 103af88 + 20a6444 commit 959d8c2

20 files changed

+300
-38
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<PackageReference Include="BootstrapBlazor.ChatBot" Version="9.0.0" />
3434
<PackageReference Include="BootstrapBlazor.CherryMarkdown" Version="9.0.1" />
3535
<PackageReference Include="BootstrapBlazor.Dock" Version="9.0.0" />
36-
<PackageReference Include="BootstrapBlazor.DockView" Version="9.1.14" />
36+
<PackageReference Include="BootstrapBlazor.DockView" Version="9.1.16" />
3737
<PackageReference Include="BootstrapBlazor.DriverJs" Version="9.0.3" />
3838
<PackageReference Include="BootstrapBlazor.ElementIcon" Version="9.0.3" />
3939
<PackageReference Include="BootstrapBlazor.FileViewer" Version="9.0.0" />

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private async Task OnSendAsync()
8383
// 记录日志
8484
_items.Add(new ConsoleMessageItem()
8585
{
86-
Message = $"{DateTime.Now}: 发送数据 {_client.LocalEndPoint} - {_serverEndPoint} Data {BitConverter.ToString(data)} {state}"
86+
Message = $"{DateTime.Now}: 发送数据 {_client.LocalEndPoint} - {_serverEndPoint} Data: {BitConverter.ToString(data)} {state}"
8787
});
8888
}
8989
}
@@ -109,7 +109,7 @@ private async Task UpdateReceiveLog(ReadOnlyMemory<byte> data)
109109

110110
_items.Add(new ConsoleMessageItem
111111
{
112-
Message = $"{DateTime.Now}: 接收数据 {_client.LocalEndPoint} - {_serverEndPoint} Data {payload} HEX: {body}",
112+
Message = $"{DateTime.Now}: 接收数据 {_client.LocalEndPoint} - {_serverEndPoint} Data: {payload} HEX: {body}",
113113
Color = Color.Success
114114
});
115115

src/BootstrapBlazor.Server/Components/Samples/Sockets/Receives.razor renamed to src/BootstrapBlazor.Server/Components/Samples/Sockets/AutoReceives.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@page "/socket/receive"
2-
@inject IStringLocalizer<Receives> Localizer
1+
@page "/socket/auto-receive"
2+
@inject IStringLocalizer<AutoReceives> Localizer
33

44
<h3>@Localizer["ReceivesTitle"]</h3>
55
<h4>@Localizer["ReceivesDescription"]</h4>

src/BootstrapBlazor.Server/Components/Samples/Sockets/Receives.razor.cs renamed to src/BootstrapBlazor.Server/Components/Samples/Sockets/AutoReceives.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BootstrapBlazor.Server.Components.Samples.Sockets;
1010
/// <summary>
1111
/// 接收电文示例
1212
/// </summary>
13-
public partial class Receives : IDisposable
13+
public partial class AutoReceives : IDisposable
1414
{
1515
[Inject, NotNull]
1616
private ITcpSocketFactory? TcpSocketFactory { get; set; }
@@ -29,7 +29,7 @@ protected override void OnInitialized()
2929
base.OnInitialized();
3030

3131
// 从服务中获取 Socket 实例
32-
_client = TcpSocketFactory.GetOrCreate("demo-receive", options =>
32+
_client = TcpSocketFactory.GetOrCreate("demo-auto-receive", options =>
3333
{
3434
options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
3535
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@page "/socket/manual-receive"
2+
@inject IStringLocalizer<ManualReceives> Localizer
3+
4+
<h3>@Localizer["ReceivesTitle"]</h3>
5+
<h4>@Localizer["ReceivesDescription"]</h4>
6+
7+
<Notice></Notice>
8+
9+
<DemoBlock Title="@Localizer["NormalTitle"]"
10+
Introduction="@Localizer["NormalIntro"]"
11+
Name="Normal" ShowCode="false">
12+
<p>本例中连接一个模拟时间同步服务,采用一发一收的方式进行通讯,连接后发送查询电文,接收到服务器端响应时间戳电文数据</p>
13+
<ul class="ul-demo">
14+
<li>点击 <b>连接</b> 按钮后通过 <code>ITcpSocketFactory</code> 服务实例创建的 <code>ITcpSocketClient</code> 对象连接到网站模拟 <code>TcpServer</code></li>
15+
<li>点击 <b>断开</b> 按钮调用 <code>CloseAsync</code> 方法断开 Socket 连接</li>
16+
<li>点击 <b>发送</b> 按钮调用 <code>SendAsync</code> 方法发送请求数据</li>
17+
</ul>
18+
<p>使用 <code>ReceiveAsync</code> 方法主动接收数据</p>
19+
<div class="row form-inline g-3">
20+
<div class="col-12 col-sm-6">
21+
<Button Text="连接" Icon="fa-solid fa-play"
22+
OnClick="OnConnectAsync" IsDisabled="@_client.IsConnected"></Button>
23+
<Button Text="断开" Icon="fa-solid fa-stop" class="ms-2"
24+
OnClick="OnCloseAsync" IsDisabled="@(!_client.IsConnected)"></Button>
25+
<Button Text="发送" Icon="fa-solid fa-paper-plane" class="ms-2" IsAsync="true"
26+
OnClick="OnSendAsync" IsDisabled="@(!_client.IsConnected)"></Button>
27+
</div>
28+
<div class="col-12">
29+
<Console Items="@_items" Height="496" HeaderText="接收数据(间隔 10 秒)"
30+
ShowAutoScroll="true" OnClear="@OnClear"></Console>
31+
</div>
32+
</div>
33+
</DemoBlock>
34+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
using System.Text;
8+
9+
namespace BootstrapBlazor.Server.Components.Samples.Sockets;
10+
11+
/// <summary>
12+
/// 接收电文示例
13+
/// </summary>
14+
public partial class ManualReceives
15+
{
16+
[Inject, NotNull]
17+
private ITcpSocketFactory? TcpSocketFactory { get; set; }
18+
19+
private ITcpSocketClient _client = null!;
20+
21+
private List<ConsoleMessageItem> _items = [];
22+
23+
private readonly IPEndPoint _serverEndPoint = new(IPAddress.Loopback, 8810);
24+
25+
/// <summary>
26+
/// <inheritdoc/>
27+
/// </summary>
28+
protected override void OnInitialized()
29+
{
30+
base.OnInitialized();
31+
32+
// 从服务中获取 Socket 实例
33+
_client = TcpSocketFactory.GetOrCreate("demo-manual-receive", options =>
34+
{
35+
options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
36+
options.IsAutoReceive = false;
37+
});
38+
}
39+
40+
private async Task OnConnectAsync()
41+
{
42+
if (_client is { IsConnected: false })
43+
{
44+
await _client.ConnectAsync(_serverEndPoint, CancellationToken.None);
45+
}
46+
}
47+
48+
private async Task OnCloseAsync()
49+
{
50+
if (_client is { IsConnected: true })
51+
{
52+
await _client.CloseAsync();
53+
}
54+
}
55+
56+
private Task OnClear()
57+
{
58+
_items = [];
59+
return Task.CompletedTask;
60+
}
61+
62+
private async Task OnSendAsync()
63+
{
64+
if (_client is { IsConnected: true })
65+
{
66+
// 准备通讯数据
67+
var data = new byte[2] { 0x01, 0x02 };
68+
var result = await _client.SendAsync(data, CancellationToken.None);
69+
var state = result ? "成功" : "失败";
70+
71+
// 记录日志
72+
_items.Add(new ConsoleMessageItem()
73+
{
74+
Message = $"{DateTime.Now}: 发送数据 {_client.LocalEndPoint} - {_serverEndPoint} Data: {BitConverter.ToString(data)} {state}"
75+
});
76+
77+
if (result)
78+
{
79+
var buffer = await _client.ReceiveAsync(CancellationToken.None);
80+
var payload = buffer.ToArray();
81+
_items.Add(new ConsoleMessageItem()
82+
{
83+
Message = $"{DateTime.Now}: 接收数据 {_client.LocalEndPoint} - {_serverEndPoint} Data {Encoding.UTF8.GetString(payload)} HEX: {BitConverter.ToString(payload)} 成功",
84+
Color = Color.Success
85+
});
86+
}
87+
}
88+
}
89+
}

src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static List<MenuItem> GenerateMenus(this IStringLocalizer<NavMenu> Locali
8686
item = new DemoMenuItem()
8787
{
8888
Text = Localizer["SocketComponents"],
89-
Icon = "fa-fw fa-solid fa-square-binary text-danger"
89+
Icon = "fa-fw fa-solid fa-satellite-dish text-danger"
9090
};
9191
AddSocket(item);
9292

@@ -210,13 +210,19 @@ void AddSocket(DemoMenuItem item)
210210
new()
211211
{
212212
IsNew = true,
213-
Text = Localizer["SocketReceive"],
214-
Url = "socket/receive"
213+
Text = Localizer["SocketManualReceive"],
214+
Url = "socket/manual-receive"
215215
},
216216
new()
217217
{
218218
IsNew = true,
219-
Text = Localizer["SocketDataAdapter"],
219+
Text = Localizer["SocketAutoReceive"],
220+
Url = "socket/auto-receive"
221+
},
222+
new()
223+
{
224+
IsNew = true,
225+
Text = Localizer["DataPackageAdapter"],
220226
Url = "socket/adapter"
221227
}
222228
};

src/BootstrapBlazor.Server/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void Invoke(BootstrapBlazorOptions option)
4646
services.AddHostedService<ClearTempFilesService>();
4747
services.AddHostedService<MockOnlineContributor>();
4848
services.AddHostedService<MockReceiveSocketServerService>();
49+
services.AddHostedService<MockSendReceiveSocketServerService>();
4950
services.AddHostedService<MockCustomProtocolSocketServerService>();
5051

5152
// 增加通用服务

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,7 +4824,9 @@
48244824
"TcpSocketFactory": "ITcpSocketFactory",
48254825
"OfficeViewer": "Office Viewer",
48264826
"SocketComponents": "ITcpSocketFactory",
4827-
"SocketReceive": "Receive"
4827+
"SocketAutoReceive": "Auto Receive",
4828+
"SocketManualReceive": "Manual Receive",
4829+
"DataPackageAdapter": "DataPackageAdapter"
48284830
},
48294831
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
48304832
"TablesHeaderTitle": "Header grouping function",
@@ -7080,9 +7082,15 @@
70807082
"OfficeViewerNormalIntro": "Set the document URL for preview by configuring the <code>Url</code> value",
70817083
"OfficeViewerToastSuccessfulContent": "Office document loaded successfully"
70827084
},
7083-
"BootstrapBlazor.Server.Components.Samples.Sockets.Receives": {
7085+
"BootstrapBlazor.Server.Components.Samples.Sockets.ManualReceives": {
7086+
"ReceivesTitle": "Manual Receive",
7087+
"ReceivesDescription": "Receive data through call ReceiveAsync and display it",
7088+
"NormalTitle": "Basic usage",
7089+
"NormalIntro": "After the connection is established, the data sent by the server is received through the <code>ReceiveAsync</code> callback method. The data problems of sticking and splitting packets need to be handled by the client."
7090+
},
7091+
"BootstrapBlazor.Server.Components.Samples.Sockets.AutoReceives": {
70847092
"ReceivesTitle": "Socket Receive",
7085-
"ReceivesDescription": "Receive data through Socket and display it",
7093+
"ReceivesDescription": "Receive data through ReceivedCallBack and display it",
70867094
"NormalTitle": "Basic usage",
70877095
"NormalIntro": "After connecting, the timestamp data sent by the server is automatically received through the <code>ReceivedCallBack</code> callback method"
70887096
},

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,7 +4824,9 @@
48244824
"TcpSocketFactory": "套接字服务 ITcpSocketFactory",
48254825
"OfficeViewer": "Office 文档预览组件",
48264826
"SocketComponents": "Socket 服务",
4827-
"SocketReceive": "接收数据"
4827+
"SocketAutoReceive": "自动接收数据",
4828+
"SocketManualReceive": "手动接收数据",
4829+
"DataPackageAdapter": "数据处理器"
48284830
},
48294831
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
48304832
"TablesHeaderTitle": "表头分组功能",
@@ -7080,9 +7082,15 @@
70807082
"OfficeViewerNormalIntro": "通过设置 <code>Url</code> 值设置预览文档地址",
70817083
"OfficeViewerToastSuccessfulContent": "Office 文档加载成功"
70827084
},
7083-
"BootstrapBlazor.Server.Components.Samples.Sockets.Receives": {
7084-
"ReceivesTitle": "Socket 接收示例",
7085-
"ReceivesDescription": "通过 Socket 接收数据并且显示",
7085+
"BootstrapBlazor.Server.Components.Samples.Sockets.ManualReceives": {
7086+
"ReceivesTitle": "手动接收示例",
7087+
"ReceivesDescription": "通过调用 ReceiveAsync 接收数据并且显示",
7088+
"NormalTitle": "基本用法",
7089+
"NormalIntro": "连接后通过 <code>ReceiveAsync</code> 回调方法接收服务端发送来的数据,需要自行处理粘包分包的数据问题"
7090+
},
7091+
"BootstrapBlazor.Server.Components.Samples.Sockets.AutoReceives": {
7092+
"ReceivesTitle": "自动接收示例",
7093+
"ReceivesDescription": "通过 ReceiveCallback 接收数据并且显示",
70867094
"NormalTitle": "基本用法",
70877095
"NormalIntro": "连接后通过 <code>ReceivedCallBack</code> 回调方法自动接收服务端发送来的时间戳数据"
70887096
},

0 commit comments

Comments
 (0)