Starting from version 6.1.1(24), the pre-connection function is supported.
Generally, initiating an HTTP/HTTPS request requires completing a TCP handshake and a TLS handshake (HTTPS only) with the target server beforehand. If subsequent requests can reuse the established connection, they can transmit data directly, thereby saving time spent on handshakes. Remote Communication Kit supports the HTTP pre-connection capability. Apps can initiate a request without data transmission, eliminating the need to establish a connection for subsequent requests.
The following conditions must be met to reuse a connection:
Boosting HTTP transmission performance through pre-connection is supported on phones, PCs/2-in-1 devices, tablets, wearables, and TVs.
import { rcp } from '@kit.RemoteCommunicationKit';
export async function startConnectionOnlyByRequest() {
const session = rcp.createSession();
const request = new rcp.Request('https://example.com', 'GET');
request.connectOnly = true;
const response = await session.fetch(request);
console.info(`Request succeeded, statusCode is ${response.statusCode}`);
// This session can be used to initiate subsequent requests.
}
When the connectOnly attribute of Request is set to true, a new connection will be established even if a connection already exists.
#include "RemoteCommunicationKit/rcp.h"
#include <stdio.h>
#include <thread>
void DataRequestCallback(void *userContext, Rcp_Response *response, uint32_t errCode) {
printf("DataRequest callback, errCode: %u\n", errCode);
if (response != nullptr) {
printf("Data request succeeded, status code: %d\n", response->statusCode);
response->destroyResponse(response);
}
}
void dataTransRequest() {
uint32_t errCode = 0;
// Create a session.
Rcp_Session *session = HMS_Rcp_CreateSession(nullptr, &errCode);
printf("create session end: %d\n", errCode);
const char *kHttpServerAddress = "https://example.com";
Rcp_Request *dataRequest = HMS_Rcp_CreateRequest(kHttpServerAddress);
dataRequest->method = RCP_METHOD_GET;
// Set this request to only establish a connection.
errCode = HMS_Rcp_SetRequestConnectOnly(dataRequest, true);
Rcp_ResponseCallbackObject callback = {DataRequestCallback, nullptr};
errCode = HMS_Rcp_Fetch(session, dataRequest, &callback);
printf("session fetch end: %d\n", errCode);
// Wait for the asynchronous callback result. You can process the callback result based on your needs.
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 1000 * 3));
// Destroy the request after the request is complete.
HMS_Rcp_DestroyRequest(dataRequest);
// This session can be used to initiate subsequent requests before it is closed.
errCode = HMS_Rcp_CloseSession(&session);
printf("HMS_Rcp_CloseSession end errCode: %d\n", errCode);
}
After a request is set to only establish a connection, a new connection will be established even if a connection already exists.
Only requests initiated through the HMS_Rcp_Fetch API can reuse existing connections. Those initiated through the HMS_Rcp_FetchSync API do not support connection reuse.
Connection reuse is not supported across sessions. You can use the default session to easily initiate requests within the same session. The default session obtained through getDefaultSession (ArkTS) and that obtained through HMS_Rcp_GetDefaultSession (C API) are actually the same session in the system.
ArkTS:
import { rcp } from '@kit.RemoteCommunicationKit';
export async function startConnectionOnlyByRequest() {
const defaultSession = rcp.getDefaultSession();
const request = new rcp.Request('https://example.com', 'GET');
request.connectOnly = true;
const response = await defaultSession.fetch(request);
console.info(`Request succeeded, statusCode is ${response.statusCode}`);
// You can use the default session to initiate subsequent requests.
} C++:
#include <stdio.h>
#include <thread>
#include "RemoteCommunicationKit/rcp.h"
Rcp_Session *defaultSession = nullptr;
void DataRequestCallback(void *userContext, Rcp_Response *response, uint32_t errCode) {
printf("DataRequest callback, errCode: %u\n", errCode);
if (response != nullptr) {
printf("Data request succeeded, status code: %d\n", response->statusCode);
response->destroyResponse(response);
}
}
void dataTransRequest() {
uint32_t errCode = 0;
// Obtain the default session.
errCode = HMS_Rcp_GetDefaultSession(&defaultSession);
printf("create session end: %d\n", errCode);
const char *kHttpServerAddress = "https://example.com";
Rcp_Request *dataRequest = HMS_Rcp_CreateRequest(kHttpServerAddress);
dataRequest->method = RCP_METHOD_GET;
Rcp_ResponseCallbackObject callback = {DataRequestCallback, nullptr};
errCode = HMS_Rcp_Fetch(defaultSession, dataRequest, &callback);
printf("defaultSession fetch end: %d\n", errCode);
// Wait for the asynchronous callback result. You can process the callback result based on your needs.
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 1000 * 3));
// Destroy the request.
HMS_Rcp_DestroyRequest(dataRequest);
// You can use the default session to initiate subsequent requests.
// When the default session is no longer needed, use HMS_Rcp_CloseSession to release the strong reference to the default session.
errCode = HMS_Rcp_CloseSession(&defaultSession);
printf("HMS_Rcp_CloseSession end errCode: %d\n", errCode);
}
The system automatically maintains a default session. The default session of an app maintains a reference to the default session in the system. After all default sessions of an app are reclaimed, the default session within the system will be closed.
When the app requests a default session again, a new default session will be created within the system. This newly created session cannot reuse previous connections. If the default session needs to maintain connections over a long period, the app should maintain at least one default session object continuously.