Skip to content

Commit

Permalink
doc(Online): add online sample (#3182)
Browse files Browse the repository at this point in the history
* refactor: 重构链接逻辑

* chore: 更新配置

* refator: 精简常量

* refactor: 移除清除逻辑

* chore: 更新配置

* refactor: 精简代码

* doc: 更新 Online 显示模板

* doc: 增加时长信息列

* doc: 增加 Online 示例

* chore: bump version 8.4.0

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Mar 31, 2024
1 parent 56e2c7a commit 66f65cf
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 49 deletions.
23 changes: 22 additions & 1 deletion src/BootstrapBlazor.Server/Components/Pages/Online.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void OnAfterRender(bool firstRender)
{
try
{
await Task.Delay(2000, _cancellationTokenSource.Token);
await Task.Delay(10000, _cancellationTokenSource.Token);
BuildContext();
await InvokeAsync(StateHasChanged);
}
Expand All @@ -63,6 +63,7 @@ private void CreateTable()
{
_table.Columns.Add("ConnectionTime", typeof(DateTimeOffset));
_table.Columns.Add("LastBeatTime", typeof(DateTimeOffset));
_table.Columns.Add("Dur", typeof(TimeSpan));
_table.Columns.Add("Ip", typeof(string));
_table.Columns.Add("City", typeof(string));
_table.Columns.Add("OS", typeof(string));
Expand All @@ -81,6 +82,7 @@ private void BuildContext()
_table.Rows.Add(
item.ConnectionTime,
item.LastBeatTime,
item.LastBeatTime - item.ConnectionTime,
item.ClientInfo?.Ip ?? "",
item.ClientInfo?.City ?? "",
item.ClientInfo?.OS ?? "",
Expand All @@ -106,6 +108,25 @@ private void BuildContext()
col.FormatString = "yyyy/MM/dd HH:mm:ss";
col.Width = 118;
}
else if (col.GetFieldName() == "Dur")
{
col.FormatString = "hh\\:mm\\:ss";
col.Width = 54;
}
else if (col.GetFieldName() == "RequestUrl")
{
col.Template = v => builder =>
{
if(v is IDynamicObject val)
{
var url = val.GetValue("RequestUrl")?.ToString();
if(!string.IsNullOrEmpty(url))
{
builder.AddContent(0, new MarkupString($"<a href=\"{url}\" target=\"_blank\">{url}</a>"));
}
}
};
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ void AddBootstrapBlazorUtility(DemoMenuItem item)
{
Text = Localizer["JSExtension"],
Url = "js-extensions"
},
new()
{
Text = Localizer["OnlineText"],
Url = "online"
}
};
AddBadge(item);
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -4632,7 +4632,8 @@
"Holiday": "ICalendarHoliday",
"Festival": "ICalendarFestival",
"Lookup": "ILookupService",
"DialogService": "DialogService"
"DialogService": "DialogService",
"OnlineText": "Online Users"
},
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
"TablesHeaderTitle": "Header grouping function",
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -4632,7 +4632,8 @@
"Holiday": "假日服务 ICalendarHoliday",
"Festival": "节日服务 ICalendarFestival",
"Lookup": "外键数据源服务 ILookupService",
"DialogService": "弹窗服务 DialogService"
"DialogService": "弹窗服务 DialogService",
"OnlineText": "在线统计 Online"
},
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
"TablesHeaderTitle": "表头分组功能",
Expand Down
6 changes: 3 additions & 3 deletions src/BootstrapBlazor.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
},
"ConnectionHubOptions": {
"Enable": true,
"ExpirationScanFrequency": "00:10:00",
"TimeoutInterval": "00:00:30",
"BeatInterval": "00:00:05"
"ExpirationScanFrequency": "00:05:00",
"TimeoutInterval": "00:03:00",
"BeatInterval": "00:00:30"
},
"IpLocatorOptions": {
"EnableCache": true,
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.3.10</Version>
<Version>8.4.0</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
53 changes: 24 additions & 29 deletions src/BootstrapBlazor/wwwroot/modules/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import EventHandler from "./event-handler.js?v=$version";

export async function init(id, options) {
const { invoke, method, interval = 3000, url, connectionId } = options;
const localStorageKey = 'bb_hub_el_id';
if (localStorage.getItem(localStorageKey) === null) {
localStorage.setItem(localStorageKey, id);
const elKey = 'bb_hub_el_id';
if (localStorage.getItem(elKey) === null) {
localStorage.setItem(elKey, id);
}

const localStorageConnectionIdKey = 'bb_hub_connection_id';
let clientId = localStorage.getItem(localStorageConnectionIdKey);
const connectionIdKey = 'bb_hub_connection_id';
let clientId = localStorage.getItem(connectionIdKey);
if (clientId === null) {
localStorage.setItem(localStorageConnectionIdKey, connectionId);
localStorage.setItem(connectionIdKey, connectionId);
clientId = connectionId;
}
window.addEventListener('unload', () => {
dispose(id);
});

const hubs = [];
const chanel = new BroadcastChannel('bb_hubs_chanel');
Expand All @@ -27,44 +24,42 @@ export async function init(id, options) {
hubs.push(id);
}
else if (type === 'dispose') {
const index = hubs.indexOf(v => v === id);
const index = hubs.indexOf(id);
if (index > -1) {
hubs.splice(index, 1);
}
if (clientId === connectionId) {
localStorage.removeItem(localStorageConnectionIdKey);
}
if (localStorage.getItem(localStorageKey) === id) {
localStorage.removeItem(localStorageKey);
if (localStorage.getItem(elKey) === id) {
localStorage.removeItem(elKey);
}
}
});

const info = await getClientInfo(url);
info.id = clientId;
const handler = setInterval(async () => {

const callback = async () => {
chanel.postMessage({ id, type: 'ping' });
let hubId = localStorage.getItem(localStorageKey);

if (hubId === null) {
localStorage.setItem(localStorageKey, id);
let hubId = localStorage.getItem(elKey);
if (hubId === null || hubs.length === 0) {
localStorage.setItem(elKey, id);
hubId = id;
}
if (hubId === id) {
await invoke.invokeMethodAsync(method, info);
}
else if (hubs.length > 0) {
const h = hubs.find(v => v === hubId);
if (h === void 0) {
localStorage.removeItem(localStorageKey);
}
}
else {
localStorage.removeItem(localStorageKey);
}
}
await callback();

const handler = setInterval(async () => {
await callback();
}, interval);

const hub = { handler, chanel, connectionId };
window.addEventListener('unload', () => {
dispose(id);
});

const hub = { handler, chanel };
Data.set(id, hub);
}

Expand Down
24 changes: 12 additions & 12 deletions test/UnitTest/Components/CalendarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,31 @@ public async Task ButtonClick_Ok()
var buttons = cut.FindAll(".calendar-button-group button");
await cut.InvokeAsync(() =>
{
// btn 上一月
buttons[1].Click();
// btn 上一年
buttons[0].Click();
});
Assert.Contains($"{DateTime.Now.AddMonths(-1).Year}{DateTime.Now.AddMonths(-1).Month} 月", cut.Find(".calendar-title").ToMarkup());
Assert.Equal(v, DateTime.Today.AddMonths(-1));
Assert.Contains($"{DateTime.Now.Year - 1}{DateTime.Now.Month} 月", cut.Find(".calendar-title").ToMarkup());
Assert.Equal(v, DateTime.Today.AddYears(-1));

await cut.InvokeAsync(() =>
{
// btn 下一月
buttons[3].Click();
// btn 下一年
buttons[4].Click();
});
Assert.Contains($"{DateTime.Now.Year}{DateTime.Now.Month} 月", cut.Find(".calendar-title").ToMarkup());

await cut.InvokeAsync(() =>
{
// btn 上一年
buttons[0].Click();
// btn 上一月
buttons[1].Click();
});
Assert.Contains($"{DateTime.Now.Year - 1}{DateTime.Now.Month} 月", cut.Find(".calendar-title").ToMarkup());
Assert.Equal(v, DateTime.Today.AddYears(-1));
Assert.Contains($"{DateTime.Now.AddMonths(-1).Year}{DateTime.Now.AddMonths(-1).Month} 月", cut.Find(".calendar-title").ToMarkup());
Assert.Equal(v, DateTime.Today.AddMonths(-1));

await cut.InvokeAsync(() =>
{
// btn 下一年
buttons[4].Click();
// btn 下一月
buttons[3].Click();
});
Assert.Contains($"{DateTime.Now.Year}{DateTime.Now.Month} 月", cut.Find(".calendar-title").ToMarkup());

Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Components/DateTimeRangeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ await cut.InvokeAsync(() =>
});

var value = cut.Instance.Value;
var startDate = DateTime.Today.AddMonths(-1).AddDays(1 - DateTime.Today.Day);
var startDate = DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(-1);
var endDate = startDate.AddDays(7).AddSeconds(-1);
Assert.Equal(startDate, value.Start);
Assert.Equal(endDate, value.End);
Expand Down

0 comments on commit 66f65cf

Please sign in to comment.