• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 流量管理
2
3## 简介
4
5流量管理提供了基于物理网络的数据流量统计能力,支持基于网卡/UID 的流量统计。
6
7流量管理主要实现功能有:
8
9- 支持基于网卡/UID 的实时流量统计。
10- 支持基于网卡/UID 的历史流量统计。
11- 支持基于网卡/UID 的流量变化订阅。
12
13> **说明:**
14> 为了保证应用的运行效率,大部分 API 调用都是异步的,对于异步调用的 API 均提供了 callback 和 Promise 两种方式,以下示例均采用 callback 函数,更多方式可以查阅[API 参考](../reference/apis-network-kit/js-apis-net-statistics.md)。
15
16以下分别介绍具体开发方式。
17
18## 接口说明
19
20完整的 JS API 说明以及实例代码请参考:[statistics 链接](../reference/apis-network-kit/js-apis-net-statistics.md)。
21
22| 接口名                                                                                      | 描述                         |
23| ------------------------------------------------------------------------------------------- | ---------------------------- |
24| getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void;                       | 获取指定网卡实时下行流量数据。 |
25| getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void;                       | 获取指定网卡实时上行流量数据。 |
26| getCellularRxBytes(callback: AsyncCallback\<number>): void;                                 | 获取蜂窝实时下行流量数据。     |
27| getCellularTxBytes(callback: AsyncCallback\<number>): void;                                 | 获取蜂窝实时上行流量数据。     |
28| getAllRxBytes(callback: AsyncCallback\<number>): void;                                      | 获取所有网卡实时下行流量数据。 |
29| getAllTxBytes(callback: AsyncCallback\<number>): void;                                      | 获取所有网卡实时上行流量数据。 |
30| getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void;                         | 获取指定应用实时下行流量数据。 |
31| getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void;                         | 获取指定应用实时上行流量数据。 |
32| getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; | 获取指定网卡历史流量信息。     |
33| getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void;       | 获取指定应用历史流量信息。     |
34| getSockfdRxBytes(sockfd: number, callback: AsyncCallback\<number>): void;                   | 获取指定socket实时下行流量数据。 |
35| getSockfdTxBytes(sockfd: number, callback: AsyncCallback\<number>): void;                   | 获取指定socket实时上行流量数据。 |
36| on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void      | 订阅流量改变事件通知。         |
37| off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void;   | 取消订阅流量改变事件通知。     |
38
39## 获取网卡/UID 的实时流量统计数据
40
411. 获取指定网卡实时实时流量数据。
422. 获取蜂窝实时实时流量数据。
433. 获取所有网卡实时实时流量数据。
444. 获取指定应用实时实时流量数据。
455. 获取指定socket实时流量数据。
46
47```ts
48// 从@ohos.net.statistics中导入statistics命名空间
49import statistics from '@ohos.net.statistics';
50import { BusinessError } from '@ohos.base';
51import socket from "@ohos.net.socket";
52
53// 获取指定网卡实时下行流量数据。
54statistics.getIfaceRxBytes('wlan0', (error: BusinessError, stats: number) => {
55  console.log(JSON.stringify(error));
56  console.log(JSON.stringify(stats));
57});
58
59// 获取指定网卡实时上行流量数据。
60statistics.getIfaceTxBytes('wlan0', (error: BusinessError, stats: number) => {
61  console.log(JSON.stringify(error));
62  console.log(JSON.stringify(stats));
63});
64
65// 获取蜂窝实时下行流量数据。
66statistics.getCellularRxBytes((error: BusinessError, stats: number) => {
67  console.log(JSON.stringify(error));
68  console.log(JSON.stringify(stats));
69});
70
71// 获取蜂窝实时上行流量数据。
72statistics.getCellularTxBytes((error: BusinessError, stats: number) => {
73  console.log(JSON.stringify(error));
74  console.log(JSON.stringify(stats));
75});
76
77// 获取所有网卡实时下行流量数据。
78statistics.getAllRxBytes((error: BusinessError, stats: number) => {
79  console.log(JSON.stringify(error));
80  console.log(JSON.stringify(stats));
81});
82
83// 获取所有网卡实时上行流量数据。
84statistics.getAllTxBytes((error: BusinessError, stats: number) => {
85  console.log(JSON.stringify(error));
86  console.log(JSON.stringify(stats));
87});
88
89// 获取指定应用实时下行流量数据。
90let uid = 20010038;
91statistics.getUidRxBytes(uid, (error: BusinessError, stats: number) => {
92  console.log(JSON.stringify(error));
93  console.log(JSON.stringify(stats));
94});
95
96// 获取指定应用实时上行流量数据。
97let uids = 20010038;
98statistics.getUidTxBytes(uids, (error: BusinessError, stats: number) => {
99  console.log(JSON.stringify(error));
100  console.log(JSON.stringify(stats));
101});
102
103// 获取指定socket实时下行流量数据。
104let tcp: socket.TCPSocket = socket.constructTCPSocketInstance();
105tcp.getSocketFd().then((sockfd: number) => {
106  statistics.getSockfdRxBytes(sockfd, (error: BusinessError, stats: number) => {
107    console.log(JSON.stringify(error));
108    console.log(JSON.stringify(stats));
109  });
110});
111
112// 获取指定socket实时上行流量数据。
113tcp.getSocketFd().then((sockfd: number) => {
114  statistics.getSockfdTxBytes(sockfd, (error: BusinessError, stats: number) => {
115    console.log(JSON.stringify(error));
116    console.log(JSON.stringify(stats));
117  });
118});
119```
120
121## 获取网卡/UID 的历史流量统计数据
122
1231. 获取指定网卡历史流量信息。
1242. 获取指定应用历史流量信息。
125
126```ts
127import statistics from '@ohos.net.statistics';
128import { BusinessError } from '@ohos.base';
129
130class IfaceInfo {
131  iface: string = "wlan0"
132  startTime: number = 1685948465
133  endTime: number = 16859485670
134}
135// 获取指定网卡历史流量信息。
136statistics.getTrafficStatsByIface(new IfaceInfo(), (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
137  console.log(JSON.stringify(error))
138  console.log("getTrafficStatsByIface bytes of received = " + JSON.stringify(statsInfo.rxBytes));
139  console.log("getTrafficStatsByIface bytes of sent = " + JSON.stringify(statsInfo.txBytes));
140  console.log("getTrafficStatsByIface packets of received = " + JSON.stringify(statsInfo.rxPackets));
141  console.log("getTrafficStatsByIface packets of sent = " + JSON.stringify(statsInfo.txPackets));
142});
143
144class UidInfo {
145  uid: number = 20010037
146  ifaceInfo: IfaceInfo = new IfaceInfo()
147}
148
149let uidInfo = new UidInfo()
150
151// 获取指定应用历史流量信息。
152statistics.getTrafficStatsByUid(uidInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
153  console.log(JSON.stringify(error))
154  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
155  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
156  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
157  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
158});
159```
160
161## 订阅流量变化事件
162
1631. 订阅流量改变事件通知。
1642. 取消订阅流量改变事件通知。
165
166```ts
167import statistics from '@ohos.net.statistics';
168class Data {
169  iface: string = ""
170  uid?: number = 0
171}
172
173let callback = (data: Data) => {
174  console.log('on netStatsChange, data:' + JSON.stringify(data));
175};
176// 订阅流量改变事件通知。
177statistics.on('netStatsChange', callback);
178
179// 取消订阅流量改变事件通知。可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
180statistics.off('netStatsChange', callback);
181statistics.off('netStatsChange');
182```
183