Skip to content

Commit 65d9737

Browse files
authored
feat(WebClientService): make client info id consistent with webclient id (#4385)
* refactor: 更改 IRootComponentGenerator 接口方法 * refactor: 更新示例 * Revert "refactor: 更改 IRootComponentGenerator 接口方法" This reverts commit e9d8e6a. * doc: 更新注释 * refactor: 增加 GetClientId 方法 * doc: 更新示例 * test: 更新单元测试 * refactor: 更新脚本 * refactor: 撤销新增方法 * doc: 更新示例 * test: 移除单元测试 * refactor: 精简代码
1 parent d85da3f commit 65d9737

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/BootstrapBlazor.Server/Components/Pages/Online.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<h4>@Localizer["SubTitle"]:@ConnectionService.Connections.Count</h4>
66

77
<section class="mt-3">
8-
<Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext" IsBordered="true" IsStriped="true"></Table>
8+
<Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext" IsBordered="true" IsStriped="true" SetRowClassFormatter="SetRowClassFormatter"></Table>
99
</section>

src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ public partial class Online : IDisposable
1515
[NotNull]
1616
private IConnectionService? ConnectionService { get; set; }
1717

18+
[Inject]
19+
[NotNull]
20+
private WebClientService? WebClientService { get; set; }
21+
1822
private DynamicObjectContext? DataTableDynamicContext { get; set; }
1923

2024
private readonly DataTable _table = new();
2125

2226
private CancellationTokenSource? _cancellationTokenSource = null;
27+
private string? _clientId;
2328

2429
/// <summary>
2530
/// <inheritdoc/>
@@ -29,7 +34,6 @@ protected override void OnInitialized()
2934
base.OnInitialized();
3035

3136
CreateTable();
32-
BuildContext();
3337
}
3438

3539
/// <summary>
@@ -44,8 +48,9 @@ protected override void OnAfterRender(bool firstRender)
4448
{
4549
Task.Run(async () =>
4650
{
47-
await Task.Delay(500);
48-
_cancellationTokenSource = new();
51+
var client = await WebClientService.GetClientInfo();
52+
_clientId = client.Id;
53+
_cancellationTokenSource ??= new();
4954
while (_cancellationTokenSource is { IsCancellationRequested: false })
5055
{
5156
try
@@ -62,6 +67,7 @@ protected override void OnAfterRender(bool firstRender)
6267

6368
private void CreateTable()
6469
{
70+
_table.Columns.Add("Id", typeof(string));
6571
_table.Columns.Add("ConnectionTime", typeof(DateTimeOffset));
6672
_table.Columns.Add("LastBeatTime", typeof(DateTimeOffset));
6773
_table.Columns.Add("Dur", typeof(TimeSpan));
@@ -81,6 +87,7 @@ private void BuildContext()
8187
foreach (var item in ConnectionService.Connections)
8288
{
8389
_table.Rows.Add(
90+
item.Id,
8491
item.ConnectionTime,
8592
item.LastBeatTime,
8693
item.LastBeatTime - item.ConnectionTime,
@@ -100,7 +107,11 @@ private void BuildContext()
100107
DataTableDynamicContext = new DataTableDynamicContext(_table, (context, col) =>
101108
{
102109
col.Text = Localizer[col.GetFieldName()];
103-
if (col.GetFieldName() == "ConnectionTime")
110+
if (col.GetFieldName() == "Id")
111+
{
112+
col.Ignore = true;
113+
}
114+
else if (col.GetFieldName() == "ConnectionTime")
104115
{
105116
col.FormatString = "yyyy/MM/dd HH:mm:ss";
106117
col.Width = 118;
@@ -150,6 +161,12 @@ private static string FormatIp(object v)
150161
return ret;
151162
}
152163

164+
private string? SetRowClassFormatter(DynamicObject context)
165+
{
166+
var id = context.GetValue("id")?.ToString();
167+
return _clientId == id ? "active" : null;
168+
}
169+
153170
private void Dispose(bool disposing)
154171
{
155172
if (disposing)

src/BootstrapBlazor/Services/WebClientService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public async ValueTask DisposeAsync()
111111
public class ClientInfo
112112
{
113113
/// <summary>
114-
/// 获得/设置 操作日志主键ID
114+
/// 获得/设置 链接 Id
115115
/// </summary>
116116
public string? Id { get; set; }
117117

src/BootstrapBlazor/wwwroot/modules/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function getClientInfo(url) {
2323
});
2424
if (result) {
2525
data.ip = result.Ip;
26-
data.id = result.Id;
2726
}
27+
data.id = localStorage.getItem('bb_hub_connection_id') ?? result.Id;
2828
return data;
2929
}

0 commit comments

Comments
 (0)