Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Example

Updated on 2024-01-19 GMT+08:00

Scenarios

This section describes the sequence of calling APIs for establishing a WebSocket connection, subscribing to information, pushing information, and maintaining connection.

Service Process

The following figure shows the WebSocket connection establishment, subscription, and information push process.

Figure 1 Process of pushing WebSocket messages
  1. A third-party application obtains a meeting control token and the server URL required for establishing a WebSocket connection using the meeting ID and host password. For details, see Obtaining a Meeting Control Token.
  2. The third-party application uses the meeting control token to obtain a token for establishing a WebSocket connection. For details, see Obtaining a Token for Establishing a WebSocket Connection.
  3. The third-party application establishes a WebSocket connection with the server.
  4. The third-party application subscribes to information that needs to be pushed by the Huawei Cloud Meeting server.
  5. The Huawei Cloud Meeting server pushes the subscribed-to information.
  6. The third-party application sends a heartbeat message at least once within 180 seconds.

Code Example 1

Code for WebSocket connection establishment, subscription, and message delivery and reception

<!DOCTYPE HTML>
<html>
   <head>
   <meta charset="utf-8">
   <title>WebSocket Connection Establishment and Message Delivery and Receiption</title>
      <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               // Establish a WebSocket connection.
		var confID = "900964776";
		var tempToken = "&tmpToken=" + "Obtaining a Token for Establishing a WebSocket Connection";
		var uri = "wss://100.94.23.40:443/cms/open/websocket/confctl/increment/conn?confID=" + confID + tempToken;
               var ws = new WebSocket(uri);

               ws.onopen = function()
               {
                // WebSocket connection established. Send a subscription message.
		var senddata = JSON.stringify({
			sequence: "000000000000000002611382273415",
			action: "Subscribe",
			data: JSON.stringify({
			subscribeType: [
			"ConfBasicInfoNotify",
			"ConfDynamicInfoNotify",
			"ParticipantsNotify",
			"AttendeesNotify",
			"SpeakerChangeNotify",
			"NetConditionNotify",
			"CustomMultiPicNotify",
			"InviteResultNotify",
			"InterpreterGroupNotify",
			"NetworkQualityNotify"
			],
			confToken: "Obtaining a Meeting Control Token",
			}),
		  });
                  ws.send(senddata);
                  alert("Sending subscription message..." + senddata);
               };

               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  // alert("Data received." + received_msg);
                };                          
            }
            else
            {
               // The browser does not support WebSocket.
               alert("Your browser does not support WebSocket.");
            }
         }
      </script>
   </head>
   <body>
      <div id="meeting">
         <a href="javascript:WebSocketTest()">WebSocket connection establishment and subscription</a>
      </div>
   </body>
</html>

Code Example 2

Code for connection establishment on a Java WebSocket client and message delivery and reception

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import com.google.gson.Gson;

public class WebSocketClientDemo extends WebSocketClient{

    public WebSocketClientDemo(String url) throws URISyntaxException {
        super(new URI(url));
    }

    @Override
    public void onOpen(ServerHandshake shake) {
        System.out.println("Hand shake!");
        for(Iterator<String> it = shake.iterateHttpFields(); it.hasNext();) {
            String key = it.next();
            System.out.println(key + ":" + shake.getFieldValue(key));
        }
    }

    @Override
    public void onMessage(String paramString) {
        System.out.println("receive msg: " + paramString);
    }

    @Override
    public void onClose(int paramInt, String paramString, Boolean paramBoolean) {
        System.out.println("Channel close!");
    }

    @Override
    public void onError(Exception e) {
        System.out.println("error: " + e);
    }

    public static void main(String[] args) throws Exception{
        try {
            // Establish a WebSocket connection.
            String confID = "900964776";
            String tempToken = "&tmpToken=" + "Obtaining a Token for Establishing a WebSocket Connection";
            String url = "wss://100.94.23.40:443/cms/open/websocket/confctl/increment/conn?confID=" + confID
                    + tempToken;
            WebSocketClientDemo client = new WebSocketClientDemo(url);
            client.connect();

            while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
                System.out.println("Not open yet");
                Thread.sleep(100);
            }

            System.out.println("WebSocket channel connected!");
            // WebSocket connection established. Send a subscription message.
            Gson gson = new Gson();

            List<SubscribeType> subscribeTypes = new ArrayList<>();
            subscribeTypes.add(SubscribeType.ConfBasicInfoNotify);
            subscribeTypes.add(SubscribeType.ConfDynamicInfoNotify);
            subscribeTypes.add(SubscribeType.ParticipantsNotify);
            subscribeTypes.add(SubscribeType.AttendeesNotify);
            subscribeTypes.add(SubscribeType.SpeakerChangeNotify);
            subscribeTypes.add(SubscribeType.NetConditionNotify);
            subscribeTypes.add(SubscribeType.InviteResultNotify);

            SubscribeReq subscribeReq = SubscribeReq.builder()
                    .subscribeType(subscribeTypes)
                    .confToken(Obtaining a Meeting Control Token)
                    .build();
            SubscribeMsgFrame subscribeMsgFrame = SubscribeMsgFrame.builder()
                    .action("Subscribe")
                    .sequence("000000000000000002611382271289")
                    .data(gson.toJson(subscribeReq))
                    .build();

            String jsonStr = gson.toJson(subscribeMsgFrame);
            client.send(jsonStr);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

Connection establishment and message delivery and reception on a Java WebSocket client
SubscribeReq.java
------
import java.util.List;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class SubscribeReq {

    /**
     * Subscription type
     */
    List<SubscribeType> subscribeType;

    /**
     * Meeting token
     */
    String confToken;
}

SubscribeMsgFrame .java
------
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class SubscribeMsgFrame {

    /**
     * Message type
     */
    String action;

    /**
     * Random sequence number of a message
     */
    String sequence;

    /**
     * Message body
     */
    String data;
}

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback