Huawei Cloud Application Middleware DCS Series—Redis Implementation (Live Video) Message Barrage

Cloud services, APIs, SDKs, debugging, viewing, I can do it all

You can learn from the short article: Redis implementation of application middleware series (video live broadcast) message barrage

1 What is DEVKIT

The Huawei Cloud Developer Plug-in (Huawei Cloud Toolkit), as an extension of Huawei Cloud's product capabilities to the developer's desktop, helps developers quickly connect to Huawei Cloud locally and connects the last mile from Huawei Cloud to developers. Huawei Cloud ToolKit is a series of IDE plug-ins built around Huawei Cloud's developer ecosystem tool system. It supports VS Code, IntelliJ IDEA and other series of platforms, as well as Huawei Cloud's self-developed CodeArts IDE, helping developers build applications more efficiently and conveniently. Committed to providing developers with a more stable, faster and safer programming experience.

Experience path: Toolkit-Huawei Cloud

2 The relationship between IDE and DEVKIT

2.1 What is CodeArts IDE?

CodeArts IDE is an integrated development environment (IDE) that combines the simplicity of a source code editor with the power of developer tools, such as code completion and debugging.

Download path: Toolkit-Huawei Cloud

2.2 The relationship between IntelliJ IDEA and DEVKIT

Installation preparation

Download and install JDK1.8 or higher.

Download and install IntelliJ IDEA 2020.2 or higher.

Installation verification

After successfully installing the plug-in on the IntelliJ series platform, you can see the Huawei Cloud Toolkit icon in the left navigation bar. Click on it and the words Huawei Cloud API will appear on the back panel, indicating that the installation is successful.

Download path: Toolkit-Huawei Cloud

2.3 The relationship between CodeArts IDE and DEVKIT

The API plug-in is a native built-in plug-in of CodeArts IDE and is installed by default. Related icons can be seen in the navigation bar on the left.

Download path: Toolkit-Huawei Cloud

2.4 Relationship between Visual Studio Code IDE and DEVKIT

Installation preparation

Download and install the latest version of Visual Studio Code.

Installation verification

After successfully installing the plug-in in VS Code IDE, you can see the Huawei Cloud Toolkit icon in the left navigation bar. Click on it and the words Huawei Cloud API will appear on the back panel, indicating that the installation is successful.

Download path: Toolkit-Huawei Cloud

3 Redis implementation (video live broadcast) message barrage example (Java version)

3.1 Use Redis to implement the ranking function

Release Notes

This example is developed based on Huawei Cloud Distributed Cache Service Redis Basic Edition 5.0.

Introduction

Redis implementation (video live broadcast) message barrage example (Java version)

The online user list in the live broadcast room, gift rankings, barrage messages and other information are all suitable for storage using the SortedSet structure in Redis.

For example, barrage messages can be sorted and returned using ZREVRANGEBYSCORE. In Redis 5.0, the zpopmax and zpopmin commands are added to make message processing more convenient.

This practice introduces how to use Redis to implement the message barrage function (video live broadcast).

Preparation before development

Register a Huawei Cloud account. For detailed instructions, please refer to How to Register as a User of the Huawei Cloud Management Console? , and complete the real-name authentication.

An ECS with a public IP address has been purchased. For details, see Purchase and log in to a Linux elastic cloud server.

A DCS cache instance has been purchased. For detailed operations, please see Purchasing a cache instance. You must ensure that the DCS instance and the purchased ECS are in the same virtual private cloud and subnet, and configure public network access to Redis on the purchased ECS. Details For details, see Using Nginx to Access Redis 4.0/5.0 Standalone/Active/Standby/Proxy Cluster Instances from the Public Network.

Install SDK

You can obtain and install the SDK through Maven. First, you need to download and install Maven in your operating system. After the installation is complete, you only need to add the corresponding dependencies to the pom.xml file of the Java project.

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

start using

Import dependent modules

//导入数据类型类
import java.util.Set;
//Redis客户端Jedis类
import redis.clients.jedis.Jedis;

Generate live broadcast room barrage data

Sample code

int num = 5;
            // 随机生成进入直播间发弹幕用户数据
            for (int i = 0; i < num; i++) {
                jedisClient.zadd(key, danmuOrder + i,
                    "message" + (i + 1) + (i + 1) + (i + 1) + (i + 1) + (i + 1) + (i + 1));
            }   

Get live broadcast room comments

Sample code

 // 获取所有新增弹幕
            Set<String> sortedDanmuList = jedisClient.zrevrangeByScore(key, danmuOrder + num, danmuOrder);
            for (String danmu : sortedDanmuList) {
                System.out.println(danmu);
            }

            System.out.println();

            System.out.println("                   " + key + "直播间最新的3条弹幕");

            Set<String> sorteDamu0List = jedisClient.zrevrangeByScore(key, danmuOrder + num, danmuOrder, 0, 3);
            for (String danmu : sorteDamu0List) {
                System.out.println(danmu);
            }

3.2 Display effect

Compile and run the above Demo program, the results are as follows:

Live barrage list

Barrage content: message-07f1add5-2f85-4309-9f31-313c860b33dc, sending time: 1686902337377

Barrage content: message-2062e817-3145-4d8b-af7f-46f334c8569c, sending time: 1686902337394

Barrage content: message-ad36a0ca-e8bd-4883-a091-e12a25c00106, sending time: 1686902337396

Barrage content: message-f02f9960-bb57-49ae-b7d8-6bd6d3ad3d14, sending time: 1686902337412

Barrage content: message-5ca39948-866e-4e54-a469-f958cae843f6, sending time: 1686902337457

Barrage content: message-5cc8b4ba-da61-4d01-9625-cf2e7337ef10, sending time: 1686902337489

Barrage content: message-15378516-18ce-4da7-bd3c-35c57dd65602, sending time: 1686902337495

Barrage content: message-1b280525-53e5-4fc6-a3e7-fb8e71eef85e, sending time: 1686902337540

Barrage content: message-adf876d1-e747-414e-92a2-397fc329bd58, sending time: 1686902337541

Barrage content: message-1d8d7901-164f-4dd4-abb4-6f2345164b0e, sending time: 1686902337582

Barrage content: message-fb35b1b4-277a-48bf-b22b-80070aae8475, sending time: 1686902337667

Barrage content: message-973b1b03-bf95-44d8-ab91-0c317b2d61b3, sending time: 1686902337755

Barrage content: message-1481f883-757d-47f7-b8c0-df024d6e64a4, sending time: 1686902337770

Barrage content: message-b79292ca-2409-43fb-aaf0-e33f3b9d9c8d, sending time: 1686902337820

Barrage content: message-66b0e955-d509-4475-9ae5-12fb86cf9596, sending time: 1686902337844

Barrage content: message-12b6d15a-037a-47ee-8294-8625d202c0a0, sending time: 1686902337907

Barrage content: message-fbc06323-da2a-44b8-874b-d2cf1a737064, sending time: 1686902337927

Barrage content: message-7a0f787c-aff1-422f-9e62-4beda0cd5914, sending time: 1686902337977

Barrage content: message-8ba5e4e0-22af-4f80-90a6-35062967e0fd, sending time: 1686902337992

Barrage content: message-fa9e1169-e918-4141-9805-87edcf84c379, sending time: 1686902338000

Barrage content: message-5d17be15-ba2e-461f-aba5-65c20c21d313, sending time: 1686902338059

Barrage content: message-dcedc840-1be7-496a-b781-5b79c2091fe5, sending time: 1686902338067

Barrage content: message-9e39eb28-6629-4d4c-8970-2acdc0e81a5c, sending time: 1686902338102

Barrage content: message-030b11fe-c258-4ca2-ac82-5e6ca1eb688f, sending time: 1686902338211

Barrage content: message-93322018-a987-47ba-8093-3937dddda97d, sending time: 1686902338242

Barrage content: message-bc04a9b0-ec83-4a24-83f6-0a4f25ee8896, sending time: 1686902338281

Barrage content: message-c6dd96d0-c938-41e4-b5d8-6275fdf83050, sending time: 1686902338290

Barrage content: message-12b70173-1b86-4370-a7ea-dc0ade135422, sending time: 1686902338312

Barrage content: message-a39c2ef8-8167-4945-b60d-355db6c69005, sending time: 1686902338318

Barrage content: message-2c3bf2fb-5298-472c-958c-c4b53d734e89, sending time: 1686902338326

The latest 5 barrage messages

Barrage content: message-2c3bf2fb-5298-472c-958c-c4b53d734e89, sending time: 1686902338326

Barrage content: message-a39c2ef8-8167-4945-b60d-355db6c69005, sending time: 1686902338318

Barrage content: message-12b70173-1b86-4370-a7ea-dc0ade135422, sending time: 1686902338312

Barrage content: message-c6dd96d0-c938-41e4-b5d8-6275fdf83050, sending time: 1686902338290

Barrage content: message-bc04a9b0-ec83-4a24-83f6-0a4f25ee8896, sending time: 1686902338281

Process finished with exit code 0

4 Experience the charm of plug-ins

Huawei Cloud devkit is now online: Toolkit-Huawei Cloud

Guess you like

Origin blog.csdn.net/hwxiaozhi/article/details/133876128