可帮助开发者在上传、下载大吞吐场景下实现网络加速。基于多网并发能力,通过系统返回的多个网络通路,支持单文件分段上传和多文件下载,需配置相关地址和权限。
| Files | Last commit | Last update |
|---|---|---|
| 1 year ago | ||
| 7 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 11 months ago | ||
| 3 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago |
Network Acceleration Based on Multi-Network Concurrency
Overview
This sample demonstrates how to leverage multi-network concurrency capabilities and utilize multiple network channels returned by the system to achieve network acceleration in high-throughput scenarios such as file upload and download.
Effect
| Download | Upload |
|---|---|
![]() |
![]() |
Precautions
- This sample cannot be run directly. To use the download feature, configure the download addresses DOWN_URL1 and DOWN_URL2 in CommonConst.ets.These two addresses must support the HEAD method and Range field on the server. To use the upload feature, configure the upload address UP_HOST in CommonConst.ets and check whether the three file upload APIs in FilePartsUploadManager.ets need modification. File segment upload uses a proprietary protocol, and the sample code is provided here for reference.
- Step 1: Call InitiateMultiPartUpload to notify the server of the initialization, passing the file path and file name. A successful call will return InitiateMultipartUpload data;
- Step 2: Use uploadSegment to upload file segments. The sample code uploads segments using the specified network and the initialization result returned in step 1, and records all returned UploadPartResult data.
- Step 3: The Push Kit merges the files by uploading all UploadPartResult data from Step 2. If you have different requirements for the above steps, for example, different returned data formats or network request headers, modify the sample code as needed.
- Step 1: Call InitiateMultiPartUpload to notify the server of the initialization, passing the file path and file name. A successful call will return InitiateMultipartUpload data;
- Enabling your application's multi-network concurrent transmission may fail due to various reasons, such as insufficient quota or unsupported multi-network concurrency. For details, see Multi-Network Concurrency Error Codes.
How to Use
- Download the sample code and import it to DevEco Studio. Open AppScope > app.json and change the value of bundleName to your application name.
- Request a profile with the "ohos.permission.LINKTURBO" permission for the application with the bundleName in step 1 by referring to Requesting ACL Permissions and Signing Your App/Atomic Service
- Go to File > Project Structure, locate the Signing Configs tab, and configure the application's signature information.
- On the device, enable Data boost under Settings > Mobile network > Network acceleration.
- After compiling and installing the application, you can use multiple network channels to either upload a single file to cloud storage in segments or download multiple files.
Project Directory
├──entry/src/main/ets
│ ├──common
│ │ └──CommonConst.ets // Common constants
│ ├──entryability
│ │ └──EntryAbility.ets // Entry ability
│ ├──entrybackupability
│ │ └──EntryBackupAbility.ets // Data backup and restoration
│ ├──net
│ │ ├──NetBoostManager.ets // Network concurrency management class
│ │ ├──NetConnectionManager.ets // Network connection obtaining
│ │ └──SocketClient.ets // Socket network request and file download
│ ├──pages
│ │ ├──DownloadPage.ets // File download page
│ │ ├──IndexPage.ets // Home page
│ │ └──UploadPage.ets // Single-file segment upload
│ ├──utils
│ │ ├──CommonUtils.ets // Common utilities, such as Toast
│ │ ├──FileUtils.ets // File utility
│ │ ├──HttpParser.ets // HTTP protocol parsing utility
│ │ ├──PhotoUtils.ets // Image utility
│ │ └──XmlUtils.ets // XML data parsing utility
│ ├──viewmodel
│ │ ├──download
│ │ │ └──FileDownManager.ets // Single-file download management
│ │ └──upload
│ │ └──FilePartsUploadManager.ets // Single-file segment upload management
│ └──view
│ ├──DownloadView.ets // Download item information
│ ├──Titles.ets // Title information
│ └──UploadView.ets // Upload item information
└──entry/src/main/resources // Static resources
How to Implement
- Use multiPathStateChange to listen for multi-network status.
- Use requestMultiPath to initiate multi-network concurrency. If the initiation succeeds, the multiPathStateChange callback is triggered, from which you can retrieve the NetHandle object.
- Use the bindSocket method of the NetHandle object to bind the socket to the corresponding network.
- Use the socket to access the network.
- Due to multi-network quota limitations, you can obtain remaining quota details by calling getMultiPathQuotaStats. When the multi-network channel is no longer in use, it is recommended to call releaseMultiPath promptly to release resources.
Required Permissions
- ohos.permission.INTERNET: allows an application to use the network.
- ohos.permission.LINKTURBO: allows an application to use network acceleration.
- ohos.permission.GET_NETWORK_INFO: allows an application to obtain network information.
Dependencies
- N/A
Constraints
- This sample is only supported on Huawei phones running standard systems.
- The HarmonyOS version must be HarmonyOS 6.0.0 Beta5 or later.
- The DevEco Studio version must be DevEco Studio 6.0.0 Beta5 or later.
- The HarmonyOS SDK version must be HarmonyOS 6.0.0 Beta5 SDK or later.
Introduction
可帮助开发者在上传、下载大吞吐场景下实现网络加速。基于多网并发能力,通过系统返回的多个网络通路,支持单文件分段上传和多文件下载,需配置相关地址和权限。
Customize your domain
