Skip to content
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/Components/Pages/Online.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<h4>@Localizer["SubTitle"]:@ConnectionService.Connections.Count</h4>

<section class="mt-3">
<Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext" IsBordered="true" IsStriped="true"></Table>
<Table TItem="DynamicObject" DynamicContext="DataTableDynamicContext" IsBordered="true" IsStriped="true" SetRowClassFormatter="SetRowClassFormatter"></Table>
</section>
25 changes: 21 additions & 4 deletions src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ 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();

private CancellationTokenSource? _cancellationTokenSource = null;
private string? _clientId;

/// <summary>
/// <inheritdoc/>
Expand All @@ -29,7 +34,6 @@ protected override void OnInitialized()
base.OnInitialized();

CreateTable();
BuildContext();
}

/// <summary>
Expand All @@ -44,8 +48,9 @@ protected override void OnAfterRender(bool firstRender)
{
Task.Run(async () =>
{
await Task.Delay(500);
_cancellationTokenSource = new();
var client = await WebClientService.GetClientInfo();
_clientId = client.Id;
_cancellationTokenSource ??= new();
while (_cancellationTokenSource is { IsCancellationRequested: false })
{
try
Expand All @@ -62,6 +67,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));
Expand All @@ -81,6 +87,7 @@ private void BuildContext()
foreach (var item in ConnectionService.Connections)
{
_table.Rows.Add(
item.Id,
item.ConnectionTime,
item.LastBeatTime,
item.LastBeatTime - item.ConnectionTime,
Expand All @@ -100,7 +107,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;
Expand Down Expand Up @@ -150,6 +161,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)
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Services/WebClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public async ValueTask DisposeAsync()
public class ClientInfo
{
/// <summary>
/// 获得/设置 操作日志主键ID
/// 获得/设置 链接 Id
/// </summary>
public string? Id { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/wwwroot/modules/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}