From e9d8e6a44740ce2cdcb4506302a69851b3a24284 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 12:55:05 +0800 Subject: [PATCH 01/12] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=20IRootCom?= =?UTF-8?q?ponentGenerator=20=E6=8E=A5=E5=8F=A3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/BaseComponents/BootstrapBlazorRoot.razor | 2 +- src/BootstrapBlazor/Services/IRootComponentGenerator.cs | 2 +- test/UnitTest/Components/BootstrapBlazorRootTest.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 71240638706..46d40618a7a 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -19,7 +19,7 @@ @foreach(var com in Generators) { - @com.Generator() + @com.Render() } @code { diff --git a/src/BootstrapBlazor/Services/IRootComponentGenerator.cs b/src/BootstrapBlazor/Services/IRootComponentGenerator.cs index 4fe99966aa9..ff59899f499 100644 --- a/src/BootstrapBlazor/Services/IRootComponentGenerator.cs +++ b/src/BootstrapBlazor/Services/IRootComponentGenerator.cs @@ -13,5 +13,5 @@ public interface IRootComponentGenerator /// 生成组件方法 /// /// - RenderFragment Generator(); + RenderFragment Render(); } diff --git a/test/UnitTest/Components/BootstrapBlazorRootTest.cs b/test/UnitTest/Components/BootstrapBlazorRootTest.cs index c6fe9636104..a970f6d0e61 100644 --- a/test/UnitTest/Components/BootstrapBlazorRootTest.cs +++ b/test/UnitTest/Components/BootstrapBlazorRootTest.cs @@ -32,7 +32,7 @@ class MockGenerator : IRootComponentGenerator /// /// /// - public RenderFragment Generator() => builder => + public RenderFragment Render() => builder => { builder.AddContent(0, new MarkupString("
")); }; From 48012f8045f5154d6b3b830d5edb7e73b2779a80 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 12:55:59 +0800 Subject: [PATCH 02/12] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Pages/Online.razor | 2 +- .../Components/Pages/Online.razor.cs | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor.Server/Components/Pages/Online.razor b/src/BootstrapBlazor.Server/Components/Pages/Online.razor index d61248981b4..52bf67c4d7b 100644 --- a/src/BootstrapBlazor.Server/Components/Pages/Online.razor +++ b/src/BootstrapBlazor.Server/Components/Pages/Online.razor @@ -5,5 +5,5 @@

@Localizer["SubTitle"]:@ConnectionService.Connections.Count

-
+
diff --git a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs index d73a4d99b71..3186cef1bd4 100644 --- a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs @@ -20,6 +20,7 @@ public partial class Online : IDisposable private readonly DataTable _table = new(); private CancellationTokenSource? _cancellationTokenSource = null; + private string? _clientId; /// /// @@ -29,23 +30,22 @@ protected override void OnInitialized() base.OnInitialized(); CreateTable(); - BuildContext(); } /// /// /// /// - protected override void OnAfterRender(bool firstRender) + protected override async Task OnAfterRenderAsync(bool firstRender) { - base.OnAfterRender(firstRender); + await base.OnAfterRenderAsync(firstRender); if (firstRender) { - Task.Run(async () => + _clientId = await ConnectionService.GetClientId(); + _ = Task.Run(async () => { - await Task.Delay(500); - _cancellationTokenSource = new(); + _cancellationTokenSource ??= new(); while (_cancellationTokenSource is { IsCancellationRequested: false }) { try @@ -62,6 +62,7 @@ protected override void OnAfterRender(bool firstRender) private void CreateTable() { + _table.Columns.Add("Id", typeof(string)); _table.Columns.Add("ConnectionTime", typeof(DateTimeOffset)); _table.Columns.Add("LastBeatTime", typeof(DateTimeOffset)); _table.Columns.Add("Dur", typeof(TimeSpan)); @@ -81,6 +82,7 @@ private void BuildContext() foreach (var item in ConnectionService.Connections) { _table.Rows.Add( + item.Id, item.ConnectionTime, item.LastBeatTime, item.LastBeatTime - item.ConnectionTime, @@ -100,7 +102,11 @@ private void BuildContext() DataTableDynamicContext = new DataTableDynamicContext(_table, (context, col) => { col.Text = Localizer[col.GetFieldName()]; - if (col.GetFieldName() == "ConnectionTime") + if (col.GetFieldName() == "Id") + { + col.Ignore = true; + } + else if (col.GetFieldName() == "ConnectionTime") { col.FormatString = "yyyy/MM/dd HH:mm:ss"; col.Width = 118; @@ -150,6 +156,12 @@ private static string FormatIp(object v) return ret; } + private string? SetRowClassFormatter(DynamicObject context) + { + var id = context.GetValue("id")?.ToString(); + return _clientId == id ? "active" : null; + } + private void Dispose(bool disposing) { if (disposing) From 709bab9f7152e9b0abd5531e036c48a447e986bb Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 13:07:15 +0800 Subject: [PATCH 03/12] =?UTF-8?q?Revert=20"refactor:=20=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=20IRootComponentGenerator=20=E6=8E=A5=E5=8F=A3=E6=96=B9?= =?UTF-8?q?=E6=B3=95"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit e9d8e6a44740ce2cdcb4506302a69851b3a24284. --- .../Components/BaseComponents/BootstrapBlazorRoot.razor | 2 +- src/BootstrapBlazor/Services/IRootComponentGenerator.cs | 2 +- test/UnitTest/Components/BootstrapBlazorRootTest.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 46d40618a7a..71240638706 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -19,7 +19,7 @@ @foreach(var com in Generators) { - @com.Render() + @com.Generator() } @code { diff --git a/src/BootstrapBlazor/Services/IRootComponentGenerator.cs b/src/BootstrapBlazor/Services/IRootComponentGenerator.cs index ff59899f499..4fe99966aa9 100644 --- a/src/BootstrapBlazor/Services/IRootComponentGenerator.cs +++ b/src/BootstrapBlazor/Services/IRootComponentGenerator.cs @@ -13,5 +13,5 @@ public interface IRootComponentGenerator /// 生成组件方法 /// /// - RenderFragment Render(); + RenderFragment Generator(); } diff --git a/test/UnitTest/Components/BootstrapBlazorRootTest.cs b/test/UnitTest/Components/BootstrapBlazorRootTest.cs index a970f6d0e61..c6fe9636104 100644 --- a/test/UnitTest/Components/BootstrapBlazorRootTest.cs +++ b/test/UnitTest/Components/BootstrapBlazorRootTest.cs @@ -32,7 +32,7 @@ class MockGenerator : IRootComponentGenerator /// /// /// - public RenderFragment Render() => builder => + public RenderFragment Generator() => builder => { builder.AddContent(0, new MarkupString("
")); }; From fa2637961db93d2b7cfd8dc6a1bc6cf6c455497a Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 13:18:44 +0800 Subject: [PATCH 04/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/WebClientService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Services/WebClientService.cs b/src/BootstrapBlazor/Services/WebClientService.cs index 78c6b4b4049..3ae2ecaf542 100644 --- a/src/BootstrapBlazor/Services/WebClientService.cs +++ b/src/BootstrapBlazor/Services/WebClientService.cs @@ -111,7 +111,7 @@ public async ValueTask DisposeAsync() public class ClientInfo { /// - /// 获得/设置 操作日志主键ID + /// 获得/设置 链接 Id /// public string? Id { get; set; } From fc4bf64a4b2b3f6de3ab7eee6f8e5820d176b7f2 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:29:08 +0800 Subject: [PATCH 05/12] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20GetClien?= =?UTF-8?q?tId=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/WebClientService.cs | 18 ++++++++++++++++++ src/BootstrapBlazor/wwwroot/modules/hub.js | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/src/BootstrapBlazor/Services/WebClientService.cs b/src/BootstrapBlazor/Services/WebClientService.cs index 3ae2ecaf542..138c404eb34 100644 --- a/src/BootstrapBlazor/Services/WebClientService.cs +++ b/src/BootstrapBlazor/Services/WebClientService.cs @@ -27,6 +27,8 @@ public class WebClientService(IIpLocatorFactory ipLocatorFactory, private ClientInfo? _client; private IIpLocatorProvider? _provider; + private JSModule? _clientModule; + /// /// 获得 ClientInfo 实例方法 /// @@ -61,6 +63,16 @@ public async Task GetClientInfo() return _client; } + /// + /// 获得 ClientId 方法 + /// + /// + public async Task GetClientId() + { + _clientModule ??= await runtime.LoadModule("./_content/BootstrapBlazor/modules/hub.js"); + return await _clientModule.InvokeAsync("getItem"); + } + /// /// SetData 方法由 JS 调用 /// @@ -91,6 +103,12 @@ protected virtual async ValueTask DisposeAsync(bool disposing) await _jsModule.DisposeAsync(); _jsModule = null; } + + if (_clientModule != null) + { + await _clientModule.DisposeAsync(); + _clientModule = null; + } } } diff --git a/src/BootstrapBlazor/wwwroot/modules/hub.js b/src/BootstrapBlazor/wwwroot/modules/hub.js index ac0923bbd8c..740ceeb102d 100644 --- a/src/BootstrapBlazor/wwwroot/modules/hub.js +++ b/src/BootstrapBlazor/wwwroot/modules/hub.js @@ -63,6 +63,10 @@ export async function init(id, options) { Data.set(id, hub); } +export function getItem(connectionIdKey = "bb_hub_connection_id") { + return localStorage.getItem(connectionIdKey); +} + export async function dispose(id) { const hub = Data.get(id); From 6c343df05fa2431103612596aaae92aaa626fc54 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:29:18 +0800 Subject: [PATCH 06/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Pages/Online.razor.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs index 3186cef1bd4..6d41fee5108 100644 --- a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs @@ -15,6 +15,10 @@ public partial class Online : IDisposable [NotNull] private IConnectionService? ConnectionService { get; set; } + [Inject] + [NotNull] + private WebClientService? WebClientService { get; set; } + private DynamicObjectContext? DataTableDynamicContext { get; set; } private readonly DataTable _table = new(); @@ -36,15 +40,15 @@ protected override void OnInitialized() /// /// /// - protected override async Task OnAfterRenderAsync(bool firstRender) + protected override void OnAfterRender(bool firstRender) { - await base.OnAfterRenderAsync(firstRender); + base.OnAfterRender(firstRender); if (firstRender) { - _clientId = await ConnectionService.GetClientId(); - _ = Task.Run(async () => + Task.Run(async () => { + _clientId = await WebClientService.GetClientId(); _cancellationTokenSource ??= new(); while (_cancellationTokenSource is { IsCancellationRequested: false }) { From cecac44fd0551e81b13a45e98378d27bb68d717a Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:36:44 +0800 Subject: [PATCH 07/12] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Services/WebClientServiceTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/UnitTest/Services/WebClientServiceTest.cs b/test/UnitTest/Services/WebClientServiceTest.cs index 9a7cc6075f6..bf23a05122b 100644 --- a/test/UnitTest/Services/WebClientServiceTest.cs +++ b/test/UnitTest/Services/WebClientServiceTest.cs @@ -84,6 +84,15 @@ public async Task SetData_Ok() Assert.Equal("192.168.0.1", client.Ip); } + [Fact] + public async Task GetClientId_Ok() + { + Context.JSInterop.Setup("getItem").SetResult("_clientId"); + var service = Context.Services.GetRequiredService(); + var id = await service.GetClientId(); + Assert.Equal("_clientId", id); + } + [Fact] public async Task WebClientService_Dispose() { From 708458ea6f12caba083f646b6c7ca174df65b626 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:41:48 +0800 Subject: [PATCH 08/12] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/wwwroot/modules/client.js | 2 +- src/BootstrapBlazor/wwwroot/modules/hub.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/BootstrapBlazor/wwwroot/modules/client.js b/src/BootstrapBlazor/wwwroot/modules/client.js index 30974525bd1..a608d7b9090 100644 --- a/src/BootstrapBlazor/wwwroot/modules/client.js +++ b/src/BootstrapBlazor/wwwroot/modules/client.js @@ -23,7 +23,7 @@ export async function getClientInfo(url) { }); if (result) { data.ip = result.Ip; - data.id = result.Id; } + data.id = localStorage.getItem('bb_hub_connection_id') ?? result.Id; return data; } diff --git a/src/BootstrapBlazor/wwwroot/modules/hub.js b/src/BootstrapBlazor/wwwroot/modules/hub.js index 740ceeb102d..ac0923bbd8c 100644 --- a/src/BootstrapBlazor/wwwroot/modules/hub.js +++ b/src/BootstrapBlazor/wwwroot/modules/hub.js @@ -63,10 +63,6 @@ export async function init(id, options) { Data.set(id, hub); } -export function getItem(connectionIdKey = "bb_hub_connection_id") { - return localStorage.getItem(connectionIdKey); -} - export async function dispose(id) { const hub = Data.get(id); From 0b8f173b8d7037158ebef57deddf36170944f1a9 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:42:00 +0800 Subject: [PATCH 09/12] =?UTF-8?q?refactor:=20=E6=92=A4=E9=94=80=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/WebClientService.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/BootstrapBlazor/Services/WebClientService.cs b/src/BootstrapBlazor/Services/WebClientService.cs index 138c404eb34..b5bd7f8e7e9 100644 --- a/src/BootstrapBlazor/Services/WebClientService.cs +++ b/src/BootstrapBlazor/Services/WebClientService.cs @@ -63,16 +63,6 @@ public async Task GetClientInfo() return _client; } - /// - /// 获得 ClientId 方法 - /// - /// - public async Task GetClientId() - { - _clientModule ??= await runtime.LoadModule("./_content/BootstrapBlazor/modules/hub.js"); - return await _clientModule.InvokeAsync("getItem"); - } - /// /// SetData 方法由 JS 调用 /// From d067c22800ca89832fa49a778fa1b7e55dcb361d Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:42:08 +0800 Subject: [PATCH 10/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs index 6d41fee5108..f6b8abe4d43 100644 --- a/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs @@ -48,7 +48,8 @@ protected override void OnAfterRender(bool firstRender) { Task.Run(async () => { - _clientId = await WebClientService.GetClientId(); + var client = await WebClientService.GetClientInfo(); + _clientId = client.Id; _cancellationTokenSource ??= new(); while (_cancellationTokenSource is { IsCancellationRequested: false }) { From f6f64974d57bc199f13dfa377285ea076fdebfb6 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:42:19 +0800 Subject: [PATCH 11/12] =?UTF-8?q?test:=20=E7=A7=BB=E9=99=A4=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Services/WebClientServiceTest.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/UnitTest/Services/WebClientServiceTest.cs b/test/UnitTest/Services/WebClientServiceTest.cs index bf23a05122b..9a7cc6075f6 100644 --- a/test/UnitTest/Services/WebClientServiceTest.cs +++ b/test/UnitTest/Services/WebClientServiceTest.cs @@ -84,15 +84,6 @@ public async Task SetData_Ok() Assert.Equal("192.168.0.1", client.Ip); } - [Fact] - public async Task GetClientId_Ok() - { - Context.JSInterop.Setup("getItem").SetResult("_clientId"); - var service = Context.Services.GetRequiredService(); - var id = await service.GetClientId(); - Assert.Equal("_clientId", id); - } - [Fact] public async Task WebClientService_Dispose() { From b0eb0471fd52266683b9f38e014d75953ef5d9af Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Mon, 30 Sep 2024 14:47:11 +0800 Subject: [PATCH 12/12] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/WebClientService.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/BootstrapBlazor/Services/WebClientService.cs b/src/BootstrapBlazor/Services/WebClientService.cs index b5bd7f8e7e9..3ae2ecaf542 100644 --- a/src/BootstrapBlazor/Services/WebClientService.cs +++ b/src/BootstrapBlazor/Services/WebClientService.cs @@ -27,8 +27,6 @@ public class WebClientService(IIpLocatorFactory ipLocatorFactory, private ClientInfo? _client; private IIpLocatorProvider? _provider; - private JSModule? _clientModule; - /// /// 获得 ClientInfo 实例方法 /// @@ -93,12 +91,6 @@ protected virtual async ValueTask DisposeAsync(bool disposing) await _jsModule.DisposeAsync(); _jsModule = null; } - - if (_clientModule != null) - { - await _clientModule.DisposeAsync(); - _clientModule = null; - } } }