Skip to content

Commit 4d58915

Browse files
committed
feat: 增加 INetworkMonitorService 服务
1 parent 5a7a8c5 commit 4d58915

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// 网络状态服务
10+
/// </summary>
11+
class DefaultNetowrkMonitorService(IJSRuntime jSRuntime)
12+
{
13+
[NotNull]
14+
private JSModule? _module = null;
15+
16+
private Task<JSModule> LoadModule() => jSRuntime.LoadUtility();
17+
18+
/// <summary>
19+
/// 获取剪切板数据方法
20+
/// </summary>
21+
public async Task<NetworkMonitorState> GetNetworkMonitorState(CancellationToken token = default)
22+
{
23+
_module ??= await LoadModule();
24+
return await _module.InvokeAsync<NetworkMonitorState?>("getNetworkInfo", token) ?? new();
25+
}
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
///
10+
/// </summary>
11+
public interface INetworkMonitorService
12+
{
13+
14+
}

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,18 @@ export function readFileAsync(file) {
907907
});
908908
}
909909

910+
export function getNetworkInfo() {
911+
if (navigator.connection) {
912+
const { downlink, effectiveType, rtt } = navigator.connection;
913+
return {
914+
downlink: downlink,
915+
networkType: effectiveType,
916+
rTT: rtt
917+
};
918+
}
919+
return null;
920+
}
921+
910922
export {
911923
autoAdd,
912924
autoRemove,

0 commit comments

Comments
 (0)