1# Traffic Management 2 3## Introduction 4 5The traffic management module allows you to query real-time or historical data traffic by the specified network interface card (NIC) or user ID (UID). 6 7Its functions include: 8 9- Obtaining real-time traffic data by NIC or UID 10- Obtaining historical traffic data by NIC or UID 11- Subscribing to traffic change events by NIC or UID 12 13> **NOTE** 14 15> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics.md). 16 17The following describes the development procedure specific to each application scenario. 18 19## Available APIs 20 21For the complete list of APIs and example code, see [Traffic Management](../reference/apis-network-kit/js-apis-net-statistics.md). 22 23| API | Description | 24|-----------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| 25| getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified NIC. | 26| getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified NIC. | 27| getCellularRxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the cellular network. | 28| getCellularTxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the cellular network. | 29| getAllRxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the all NICs. | 30| getAllTxBytes(callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the all NICs. | 31| getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified application. | 32| getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified application. | 33| <!--DelRow-->getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified NIC. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md). | 34| <!--DelRow-->getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void; | Obtains the historical data traffic of the specified application. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md). | 35| getSockfdRxBytes(sockfd: number, callback: AsyncCallback\<number>): void; | Obtains the real-time downlink data traffic of the specified socket. | 36| getSockfdTxBytes(sockfd: number, callback: AsyncCallback\<number>): void; | Obtains the real-time uplink data traffic of the specified socket. | 37| <!--DelRow-->on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void; | Subscribes to traffic change events. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md). | 38| <!--DelRow-->off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void; | Unsubscribes from traffic change events. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md). | 39| <!--DelRow-->getTrafficStatsByNetwork(networkInfo: NetworkInfo): Promise\<UidNetStatsInfo>; | Obtains the traffic statistics of all applications on the specified network within the specified period. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md).| 40| <!--DelRow-->getTrafficStatsByUidNetwork(uid: number, networkInfo: NetworkInfo): Promise\<NetStatsInfoSequence>; | Obtains the traffic statistics of the specified application on the specified network within the specified period. This is a system API. For details, see [API Reference](../reference/apis-network-kit/js-apis-net-statistics-sys.md). | 41 42## Obtaining Real-Time Traffic Data by NIC or UID 43 441. Obtain the real-time data traffic of the specified NIC. 452. Obtain the real-time data traffic of the cellular network. 463. Obtain the real-time data traffic of all NICs. 474. Obtain the real-time data traffic of the specified application. 485. Obtains the real-time data traffic of the specified socket. 49 50```ts 51// Import the statistics namespace from @kit.NetworkKit. 52import { statistics, socket } from '@kit.NetworkKit'; 53import { BusinessError } from '@kit.BasicServicesKit'; 54 55// Obtain the real-time downlink data traffic of the specified NIC. 56statistics.getIfaceRxBytes("wlan0").then((stats: number) => { 57 console.log(JSON.stringify(stats)); 58}); 59 60// Obtain the real-time uplink data traffic of the specified NIC. 61statistics.getIfaceTxBytes("wlan0").then((stats: number) => { 62 console.log(JSON.stringify(stats)); 63}); 64 65// Obtain the real-time downlink data traffic of the cellular network. 66statistics.getCellularRxBytes().then((stats: number) => { 67 console.log(JSON.stringify(stats)); 68}); 69 70// Obtain the real-time uplink data traffic of the cellular network. 71statistics.getCellularTxBytes().then((stats: number) => { 72 console.log(JSON.stringify(stats)); 73}); 74 75// Obtain the real-time downlink data traffic of the all NICs. 76statistics.getAllRxBytes().then((stats: number) => { 77 console.log(JSON.stringify(stats)); 78}); 79 80// Obtain the real-time uplink data traffic of the all NICs. 81statistics.getAllTxBytes().then((stats: number) => { 82 console.log(JSON.stringify(stats)); 83}); 84 85// Obtain the real-time downlink data traffic of the specified application. 86let uid = 20010038; 87statistics.getUidRxBytes(uid).then((stats: number) => { 88 console.log(JSON.stringify(stats)); 89}); 90 91// Obtain the real-time uplink data traffic of the specified application. 92let uids = 20010038; 93statistics.getUidTxBytes(uids).then((stats: number) => { 94 console.log(JSON.stringify(stats)); 95}); 96 97// Obtain the real-time downlink data traffic of the specified socket. 98let tcp: socket.TCPSocket = socket.constructTCPSocketInstance(); 99tcp.getSocketFd().then((sockfd: number) => { 100 statistics.getSockfdRxBytes(sockfd).then((stats: number) => { 101 console.log(JSON.stringify(stats)); 102 }).catch((err: BusinessError) => { 103 console.error(JSON.stringify(err)); 104 }); 105}); 106 107// Obtain the real-time uplink data traffic of the specified socket. 108tcp.getSocketFd().then((sockfd: number) => { 109 statistics.getSockfdTxBytes(sockfd).then((stats: number) => { 110 console.log(JSON.stringify(stats)); 111 }).catch((err: BusinessError) => { 112 console.error(JSON.stringify(err)); 113 }); 114}); 115``` 116 117<!--Del--> 118## Obtaining Historical Traffic Data by NIC or UID 119 1201. Obtain the historical data traffic of the specified NIC. 1212. Obtain the historical data traffic of the specified application. 122 123```ts 124import { statistics } from '@kit.NetworkKit'; 125import { BusinessError } from '@kit.BasicServicesKit'; 126 127class IfaceInfo { 128 iface: string = "wlan0" 129 startTime: number = 1685948465 130 endTime: number = 16859485670 131} 132// Obtain the historical data traffic of the specified NIC. 133statistics.getTrafficStatsByIface(new IfaceInfo()).then((statsInfo: statistics.NetStatsInfo) => { 134 console.log( 135 "getTrafficStatsByIface bytes of received = " + 136 JSON.stringify(statsInfo.rxBytes) 137 ); 138 console.log( 139 "getTrafficStatsByIface bytes of sent = " + 140 JSON.stringify(statsInfo.txBytes) 141 ); 142 console.log( 143 "getTrafficStatsByIface packets of received = " + 144 JSON.stringify(statsInfo.rxPackets) 145 ); 146 console.log( 147 "getTrafficStatsByIface packets of sent = " + 148 JSON.stringify(statsInfo.txPackets) 149 ); 150}); 151 152class UidInfo { 153 uid: number = 20010037 154 ifaceInfo: IfaceInfo = new IfaceInfo() 155} 156 157let uidInfo = new UidInfo() 158 159// Obtain the historical data traffic of the specified application. 160statistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => { 161 console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 162 console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 163 console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets)); 164 console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets)); 165}) 166``` 167 168## Subscribing to Traffic Change Events 169 1701. Subscribe to traffic change events. 1712. Unsubscribe from traffic change events. 172 173```ts 174import { statistics } from '@kit.NetworkKit'; 175 176class Data { 177 iface: string = "" 178 uid?: number = 0 179} 180 181let callback = (data: Data) => { 182 console.log('on netStatsChange, data:' + JSON.stringify(data)); 183}; 184// Subscribe to traffic change events. 185statistics.on('netStatsChange', callback); 186 187// Unsubscribe from traffic change events. You can pass the callback of the **on** function if you want to unsubscribe from a certain type of event. If you do not pass the callback, you will unsubscribe from all events. 188statistics.off('netStatsChange', callback); 189statistics.off('netStatsChange'); 190``` 191<!--DelEnd--> 192