目录

Transmission Performance Optimization Based on Configuration

Constraints

The capability of transmission performance optimization based on configuration supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.

Modules to Import

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

Request Preprocessing Phase

DNS Phase

An app can customize DNS request rules, such as by specifying DNS servers and overriding DNS resolution functions, to achieve the best DNS performance.

// Customize the DNS resolution function.
const session = rcp.createSession();
// 'https://example.com' is only an example. Modify it as needed.
const request = new rcp.Request('https://example.com');
request.configuration = {
  dns: {
    dnsRules: (host: string, port: number): rcp.IpAddress[] => {
      // 'example.com' is an example domain name. '192.168.1.1' and '192.168.1.2' are sample IP addresses. Modify them as needed.
      if (host === 'example.com') {
        return ['192.168.1.1', '192.168.1.2'];
      }
      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}`);
  // ...
});
// Customize a DNS server.
const session = rcp.createSession();
// 'https://example.com' is only an example. Modify it as needed.
const request = new rcp.Request('https://example.com');
request.configuration = {
  dns: {
    // '192.168.1.1' is a sample IP address, and 53 is a sample port number. Modify them as needed.
    dnsRules: [
      {
        ip: '192.168.1.1',
        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}`);
  // ...
});

Connection Phase

You can dynamically adjust the connection pool size based on resource characteristics.

const session = rcp.createSession({
  connectionConfiguration: {
    maxConnectionsPerHost: 16,
    maxTotalConnections: 1024,
  }
});
for (let i = 0; i < 1024; ++i) {
  // The URL to request is only an example. Modify it as needed.
  session.get('https://example' + 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 Request Phase

HTTP Response Phase

Obtain the time consumed at each phase of the response to dynamically determine the network quality, and adjust requests accordingly (for example, request resources of different qualities and reduce the number of cached resources). For more detailed examples, see Time Points in the HTTP Request Process.

// Obtain the consumed time of each phase.
const session = rcp.createSession({requestConfiguration: {tracing: tracingConfig}});
try {
  // 'https://example.com' is only an example. Modify it as needed.
  const response = await session.get('https://example.com');
  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();
}