Skip to content

Commit 66f65cf

Browse files
authored
doc(Online): add online sample (#3182)
* refactor: 重构链接逻辑 * chore: 更新配置 * refator: 精简常量 * refactor: 移除清除逻辑 * chore: 更新配置 * refactor: 精简代码 * doc: 更新 Online 显示模板 * doc: 增加时长信息列 * doc: 增加 Online 示例 * chore: bump version 8.4.0 * test: 更新单元测试
1 parent 56e2c7a commit 66f65cf

File tree

9 files changed

+72
-49
lines changed

9 files changed

+72
-49
lines changed

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override void OnAfterRender(bool firstRender)
4949
{
5050
try
5151
{
52-
await Task.Delay(2000, _cancellationTokenSource.Token);
52+
await Task.Delay(10000, _cancellationTokenSource.Token);
5353
BuildContext();
5454
await InvokeAsync(StateHasChanged);
5555
}
@@ -63,6 +63,7 @@ private void CreateTable()
6363
{
6464
_table.Columns.Add("ConnectionTime", typeof(DateTimeOffset));
6565
_table.Columns.Add("LastBeatTime", typeof(DateTimeOffset));
66+
_table.Columns.Add("Dur", typeof(TimeSpan));
6667
_table.Columns.Add("Ip", typeof(string));
6768
_table.Columns.Add("City", typeof(string));
6869
_table.Columns.Add("OS", typeof(string));
@@ -81,6 +82,7 @@ private void BuildContext()
8182
_table.Rows.Add(
8283
item.ConnectionTime,
8384
item.LastBeatTime,
85+
item.LastBeatTime - item.ConnectionTime,
8486
item.ClientInfo?.Ip ?? "",
8587
item.ClientInfo?.City ?? "",
8688
item.ClientInfo?.OS ?? "",
@@ -106,6 +108,25 @@ private void BuildContext()
106108
col.FormatString = "yyyy/MM/dd HH:mm:ss";
107109
col.Width = 118;
108110
}
111+
else if (col.GetFieldName() == "Dur")
112+
{
113+
col.FormatString = "hh\\:mm\\:ss";
114+
col.Width = 54;
115+
}
116+
else if (col.GetFieldName() == "RequestUrl")
117+
{
118+
col.Template = v => builder =>
119+
{
120+
if(v is IDynamicObject val)
121+
{
122+
var url = val.GetValue("RequestUrl")?.ToString();
123+
if(!string.IsNullOrEmpty(url))
124+
{
125+
builder.AddContent(0, new MarkupString($"<a href=\"{url}\" target=\"_blank\">{url}</a>"));
126+
}
127+
}
128+
};
129+
}
109130
});
110131
}
111132

src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ void AddBootstrapBlazorUtility(DemoMenuItem item)
123123
{
124124
Text = Localizer["JSExtension"],
125125
Url = "js-extensions"
126+
},
127+
new()
128+
{
129+
Text = Localizer["OnlineText"],
130+
Url = "online"
126131
}
127132
};
128133
AddBadge(item);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -4632,7 +4632,8 @@
46324632
"Holiday": "ICalendarHoliday",
46334633
"Festival": "ICalendarFestival",
46344634
"Lookup": "ILookupService",
4635-
"DialogService": "DialogService"
4635+
"DialogService": "DialogService",
4636+
"OnlineText": "Online Users"
46364637
},
46374638
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
46384639
"TablesHeaderTitle": "Header grouping function",

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -4632,7 +4632,8 @@
46324632
"Holiday": "假日服务 ICalendarHoliday",
46334633
"Festival": "节日服务 ICalendarFestival",
46344634
"Lookup": "外键数据源服务 ILookupService",
4635-
"DialogService": "弹窗服务 DialogService"
4635+
"DialogService": "弹窗服务 DialogService",
4636+
"OnlineText": "在线统计 Online"
46364637
},
46374638
"BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": {
46384639
"TablesHeaderTitle": "表头分组功能",

src/BootstrapBlazor.Server/appsettings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
},
4242
"ConnectionHubOptions": {
4343
"Enable": true,
44-
"ExpirationScanFrequency": "00:10:00",
45-
"TimeoutInterval": "00:00:30",
46-
"BeatInterval": "00:00:05"
44+
"ExpirationScanFrequency": "00:05:00",
45+
"TimeoutInterval": "00:03:00",
46+
"BeatInterval": "00:00:30"
4747
},
4848
"IpLocatorOptions": {
4949
"EnableCache": true,

src/BootstrapBlazor/BootstrapBlazor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>8.3.10</Version>
4+
<Version>8.4.0</Version>
55
</PropertyGroup>
66

77
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">

src/BootstrapBlazor/wwwroot/modules/hub.js

+24-29
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@ import EventHandler from "./event-handler.js?v=$version";
44

55
export async function init(id, options) {
66
const { invoke, method, interval = 3000, url, connectionId } = options;
7-
const localStorageKey = 'bb_hub_el_id';
8-
if (localStorage.getItem(localStorageKey) === null) {
9-
localStorage.setItem(localStorageKey, id);
7+
const elKey = 'bb_hub_el_id';
8+
if (localStorage.getItem(elKey) === null) {
9+
localStorage.setItem(elKey, id);
1010
}
1111

12-
const localStorageConnectionIdKey = 'bb_hub_connection_id';
13-
let clientId = localStorage.getItem(localStorageConnectionIdKey);
12+
const connectionIdKey = 'bb_hub_connection_id';
13+
let clientId = localStorage.getItem(connectionIdKey);
1414
if (clientId === null) {
15-
localStorage.setItem(localStorageConnectionIdKey, connectionId);
15+
localStorage.setItem(connectionIdKey, connectionId);
1616
clientId = connectionId;
1717
}
18-
window.addEventListener('unload', () => {
19-
dispose(id);
20-
});
2118

2219
const hubs = [];
2320
const chanel = new BroadcastChannel('bb_hubs_chanel');
@@ -27,44 +24,42 @@ export async function init(id, options) {
2724
hubs.push(id);
2825
}
2926
else if (type === 'dispose') {
30-
const index = hubs.indexOf(v => v === id);
27+
const index = hubs.indexOf(id);
3128
if (index > -1) {
3229
hubs.splice(index, 1);
3330
}
34-
if (clientId === connectionId) {
35-
localStorage.removeItem(localStorageConnectionIdKey);
36-
}
37-
if (localStorage.getItem(localStorageKey) === id) {
38-
localStorage.removeItem(localStorageKey);
31+
if (localStorage.getItem(elKey) === id) {
32+
localStorage.removeItem(elKey);
3933
}
4034
}
4135
});
4236

4337
const info = await getClientInfo(url);
4438
info.id = clientId;
45-
const handler = setInterval(async () => {
39+
40+
const callback = async () => {
4641
chanel.postMessage({ id, type: 'ping' });
47-
let hubId = localStorage.getItem(localStorageKey);
4842

49-
if (hubId === null) {
50-
localStorage.setItem(localStorageKey, id);
43+
let hubId = localStorage.getItem(elKey);
44+
if (hubId === null || hubs.length === 0) {
45+
localStorage.setItem(elKey, id);
5146
hubId = id;
5247
}
5348
if (hubId === id) {
5449
await invoke.invokeMethodAsync(method, info);
5550
}
56-
else if (hubs.length > 0) {
57-
const h = hubs.find(v => v === hubId);
58-
if (h === void 0) {
59-
localStorage.removeItem(localStorageKey);
60-
}
61-
}
62-
else {
63-
localStorage.removeItem(localStorageKey);
64-
}
51+
}
52+
await callback();
53+
54+
const handler = setInterval(async () => {
55+
await callback();
6556
}, interval);
6657

67-
const hub = { handler, chanel, connectionId };
58+
window.addEventListener('unload', () => {
59+
dispose(id);
60+
});
61+
62+
const hub = { handler, chanel };
6863
Data.set(id, hub);
6964
}
7065

test/UnitTest/Components/CalendarTest.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,31 @@ public async Task ButtonClick_Ok()
7070
var buttons = cut.FindAll(".calendar-button-group button");
7171
await cut.InvokeAsync(() =>
7272
{
73-
// btn 上一月
74-
buttons[1].Click();
73+
// btn 上一年
74+
buttons[0].Click();
7575
});
76-
Assert.Contains($"{DateTime.Now.AddMonths(-1).Year}{DateTime.Now.AddMonths(-1).Month} 月", cut.Find(".calendar-title").ToMarkup());
77-
Assert.Equal(v, DateTime.Today.AddMonths(-1));
76+
Assert.Contains($"{DateTime.Now.Year - 1}{DateTime.Now.Month} 月", cut.Find(".calendar-title").ToMarkup());
77+
Assert.Equal(v, DateTime.Today.AddYears(-1));
7878

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

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

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

test/UnitTest/Components/DateTimeRangeTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ await cut.InvokeAsync(() =>
6868
});
6969

7070
var value = cut.Instance.Value;
71-
var startDate = DateTime.Today.AddMonths(-1).AddDays(1 - DateTime.Today.Day);
71+
var startDate = DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(-1);
7272
var endDate = startDate.AddDays(7).AddSeconds(-1);
7373
Assert.Equal(startDate, value.Start);
7474
Assert.Equal(endDate, value.End);

0 commit comments

Comments
 (0)