tencent cloud

フィードバック

Data Channel

最終更新日:2024-01-26 11:54:09
    This document describes how to use a data channel to establish communication between the business client and a cloud application and transfer startup parameters, commands, messages, or other data.

    Sequence diagram

    

    Directions

    1. Create a UDP service for the cloud business application to listen for a local UDP port (recommended port range: 10000–20000 for localhost 127.0.0.1) and wait to receive UDP packets.
    2. The business client calls the TCGSDK.start() API to start the cloud application. After the application connection, the onConnectSuccess() callback is triggered. We recommend you create a data channel for the business client after this callback.
    3. The business client calls the TCGSDK.createCustomDataChannel() API to create a passthrough channel. The target port parameter in the API must be the port listened for by the cloud application in step 1. If the data channel fails to be created, repeat this step until it is created successfully.
    4. After the data channel is created successfully, the business client can call sendMessage() to send a data packet customized by the business. The UDP
    service of the cloud application receives the request and parses the UDP source address.
    
    5. The cloud application sends a custom data packet to the UDP source address obtained in step 4. The data packet will then be returned through the created data channel. The business client can process the returned packet in the onMessage() callback API.

    Sample code

    Business client (JavaScript SDK example)
    Cloud application (C/C++ example)
    let timer = null;
    
    const { sendMessage, code } = await TCGSDK.createCustomDataChannel({
    destPort: xxxx, // The recommended port range of destPort is 10000–20000.
    onMessage: (res) => {
    console.log('CustomDataChannel onMessage', res);
    // The receipt of this callback indicates that the cloud application has been successfully initiated and successfully transmitted data to the web end. You can use clearInterval and send data normally afterwards.
    // clearInterval(timer);
    },
    });
    
    // The code value 0 indicates successful establishment of a data channel between the web end and the cloud. However, the cloud application might not yet have been fully initiated (prohibiting data transmission). The web end can dispatch initial data via setTimeout/setInterval/polling, until the cloud application responds normally.
    if (code === 0) {
    // Send custom data.
    sendMessage('test');
    }
    
    if (code === 1 || code === -1) {
    // Retry upon failed creation.
    // timer = setInterval(() => {
    // sendMessage('test');
    // }, 5000);
    } else {
    // Contemplate re-establishing the data channel.
    }
    int main() {
    int udp_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_socket_fd == -1) {
    printf("socket failed!\\n");
    return -1;
    }
    
    // Set the destination IP address.
    struct sockaddr_in bind_addr = { 0 };
    bind_addr.sin_family = AF_INET;
    bind_addr.sin_port = htons(xxxx); // The recommended port range in htons(xxxx) is 10000–20000.
    bind_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); // Bind the IP
    
    // Bind the port.
    int ret = bind(udp_socket_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
    if (ret < 0) {
    perror("bind fail:");
    close(udp_socket_fd);
    return -1;
    }
    
    // Start to wait for a client message.
    struct sockaddr_in upstream_addr = { 0 }; // Address used to store the CAR proxy.
    int len = sizeof(upstream_addr);
    char buf[1024] = { 0 }; // Receive buffer.
    while (true) {
    ret = recvfrom(udp_socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&upstream_addr, &len);
    if (ret == -1) {
    break;
    }
    // buf is the message "test" sent by the frontend.
    // upstream_addr can be subsequently used to send messages to the frontend.
    const char* response = "response";
    sendto(udp_socket_fd, response, strlen(response), 0, (struct sockaddr *)&upstream_addr, sizeof(upstream_addr));
    }
    return 0;
    }

    FAQs

    The data channel is successfully created, the business frontend successfully sends data, but the response from the cloud application is not received. What should I do?

    A successful call to the TCGSDK.createCustomDataChannel() API indicates that a data channel has been successfully established between the business frontend and the cloud. However, it is possible that the cloud application may not be fully initiated at this time. The business frontend can send custom data via timeout, interval, polling, etc., to ensure that the cloud application normally receives the custom data sent by the business frontend once it is successfully initiated. As long as the data channel is successfully created, data can be sent successfully by default.
    If you confirm that the cloud application data is received in the onMessage callback, you can cancel the polling and then send the data normally.

    What types of data can be sent and received?

    The sendMessage() API supports data types such as string and ArrayBuffer.

    Is there any limit on the size of a data packet to be transferred?

    There is no limit on the size of a data packet to be transferred. However, it must be noted that the UDP packet can be of up to 64 KB and it is suggested that the packet size be kept less than MTU 1500. If the packet size is too large, it is recommended to use packet splitting for the transmission, as too much upstream data could potentially cause congestion and affect the user experience.
    
    お問い合わせ

    カスタマーサービスをご提供できるため、ぜひお気軽にお問い合わせくださいませ。

    テクニカルサポート

    さらにサポートが必要な場合は、サポートチケットを送信して弊社サポートチームにお問い合わせください。24時間365日のサポートをご提供します。

    電話サポート(24 時間365日対応)