目录

Closing a Session (C++)

When a remote communication request is completed (data has been successfully sent and acknowledged) or the communication attempt has failed due to timeout or other errors, you need to call relevant methods to close the session or release resources. This is primarily to:

Promptly closing sessions and releasing associated resources upon request completion is crucial for maintaining system health and efficiency. This not only optimizes resource utilization but also enhances system stability and reliability.

Constraints

The session closing capability supports phones, 2-in-1 devices, tablets, and wearables. Starting from version 5.1.1(19), it is also supported on TVs.

API Description

For details about the APIs, please refer to the API Reference.

API Description
uint32_t HMS_Rcp_CloseSession(Rcp_Session **session); Closes a session.

Example

  1. Import required modules on CPP.

    #include "RemoteCommunicationKit/rcp.h"
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <thread>
  2. Add the following library to CMakeLists.txt. (For details, please refer to Preparations for C API Development.)

    librcp_c.so
  3. Create a session, initiate a request, and close the session. Replace https://www.example.com with the actual URL to request. After the response is returned, destroy the request and close the session.

    const char *kHttpServerAddress = "http://www.example.com";
    Rcp_Request *request = HMS_Rcp_CreateRequest(kHttpServerAddress);
    request->method = RCP_METHOD_GET;
    uint32_t errCode = 0;
    // Create a session.
    Rcp_Session *session = HMS_Rcp_CreateSession(NULL, &errCode);
    // Configure the request callback.
    Rcp_ResponseCallbackObject responseCallback = {ResponseCallback, NULL};
    // Initiate a FETCH request.
    errCode = HMS_Rcp_Fetch(session, request, &responseCallback);
    // Cancel ongoing requests before closing the session.
    errCode = HMS_Rcp_CancelSession(session);
    // Close the session.
    errCode = HMS_Rcp_CloseSession(&session);
    napi_value value = nullptr;
    napi_create_int32(env, errCode, &value);
    napi_resolve_deferred(env, ctx->deferred, value);
    // Destroy the request.
    HMS_Rcp_DestroyRequest(request);