# 通过配置Configuration优化传输性能

#### 约束与限制

通过配置Configuration优化传输性能能力支持Phone、2in1、Tablet、Wearable设备。并且从5.1.1(19)开始，新增支持TV设备；从6.1.0(23)开始，新增支持Car设备。

#### 导入模块

```
import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit';
```

#### 请求预处理阶段

*   基于Session抽象的高并发网络框架
    
*   支持创建多个Session
    
*   支持请求动态取消
    
*   支持关闭Session
    
*   Session中的资源互相独立、互不影响
    
*   应用可以通过使用Session来获取最佳的网络性能体验
    
    ```
    const session1 = rcp.createSession({
      requestConfiguration: {
        transfer: {
          timeout: {
            connectMs: 5000,
            transferMs: 5000
          }
        }
      }
    });
    
    const request1 = new rcp.Request('');
    const request2 = new rcp.Request('');
    session1.fetch(request1).then((response) => {
      console.info(`Request1 succeeded, message is ${JSON.stringify(response)}`);
    }).catch((err: BusinessError) => {
      console.error(`err1: error code is ${err.code}, error data is ${err.data}`);
    });
    session1.fetch(request2).then((response) => {
      console.info(`Request2 succeeded, message is ${JSON.stringify(response)}`);
      session1.close();
    }).catch((err: BusinessError) => {
      console.error(`err2: error code is ${err.code}, error data is ${err.data}`);
      session1.close();
    });
    session1.cancel(request1); // 取消request1请求
    ```
    

#### DNS阶段

应用可定制DNS请求规则，如定制DNS服务器、重写DNS解析函数，从而获取最佳的DNS性能体验。

```
// 定制DNS解析函数
const session = rcp.createSession();
const request = new rcp.Request('');
request.configuration = {
  dns: {
    dnsRules: (host: string, port: number): rcp.IpAddress[] => {
      if (host === 'example.com') {
        return ['7.128.8.45', '7.128.8.46'];
      }
      return [];
    }
  }
};
session.fetch(request).then((response) => {
  console.info(`Request succeeded, message is ${JSON.stringify(response)}`);
}).catch((err: BusinessError) => {
  console.error(`err: error code is ${err.code}, error data is ${err.data}`);
});
```

```
// 定制DNS服务器
const session = rcp.createSession();
const request = new rcp.Request('');
request.configuration = {
  dns: {
    dnsRules: [
      {
        ip: '7.128.8.45',
        port: 53,
      },
    ]
  }
};
session.fetch(request).then((response) => {
  console.info(`Request succeeded, message is ${JSON.stringify(response)}`);
}).catch((err: BusinessError) => {
  console.error(`err: error code is ${err.code}, error data is ${err.data}`);
});
```

#### 连接阶段

根据资源特征动态调整连接池大小

```
const session = rcp.createSession({
  connectionConfiguration: {
    maxConnectionsPerHost: 16,
    maxTotalConnections: 1024,
  }
});
for (let i = 0; i < 1024; ++i) {
  session.get('' + i.toString() + '.com/image.png').then((response) => {
    console.info(`Request succeeded, message is ${JSON.stringify(response)}`);
  }).catch((err: BusinessError) => {
    console.error(`err: error code is ${err.code}, error data is ${err.data}`);
  });
}
```

#### HTTP请求阶段

*   支持响应体分段返回，以节省内存
    
*   支持直接将响应写入文件，以节省内存
    
*   支持请求体分段上传，以节省内存
    
    ```
    // 使用响应体直接写入文件
    const session = rcp.createSession();
    try {
      const response = await session.get('', {
        kind: 'file',
        file: './video.mp4',
      });
      console.info(`Request succeeded, message is ${JSON.stringify(response)}`);
    } catch (err) {
      console.error(`err: error code is ${err.code}, error data is ${err.data}`);
    } finally {
      session.close();
    }
    ```
    
    ```
    // 分段上传数据
    const session = rcp.createSession();
    try {
      const response = await session.post('', (maxSize: number) => {
        return new ArrayBuffer(maxSize);
      });
      console.info(`Request succeeded, message is ${JSON.stringify(response)}`);
    } catch (err) {
      console.error(`err: error code is ${err.code}, error data is ${err.data}`);
    } finally {
      session.close();
    }
    ```
    

#### HTTP响应阶段

获取响应各阶段耗时动态判断网络质量，动态调整请求（请求不同质量的资源、降低资源缓存数量）。更详细的示例请移步HTTP请求过程中各时间点详解。

```
// 获取各个阶段的耗时信息
const session = rcp.createSession();
try {
  const response = await session.get('');
  console.info(response.timeInfo?.nameLookupTimeMs.toString());
  console.info(response.timeInfo?.connectTimeMs.toString());
  console.info(response.timeInfo?.tlsHandshakeTimeMs.toString());
  console.info(response.timeInfo?.preTransferTimeMs.toString());
  console.info(response.timeInfo?.startTransferTimeMs.toString());
  console.info(response.timeInfo?.totalTimeMs.toString());
  console.info(response.timeInfo?.redirectTimeMs.toString());
} catch (err) {
  console.error(`err: error code is ${err.code}, error data is ${err.data}`);
} finally {
  session.close();
}
```