Real-Time Signaling

This section describes how to rapidly integrate a Hummer SDK Android version to the project.

1. Try Demo

Open-source example project
Try Demo

2. Prerequisites

3. Development Environment Preparation

This section describes how to create a project and integrate a Hummer SDK to your project.

3.1 Step 1: Create a Project

  1. Open Android Studio and click Start a New Android Studio project.
  2. On the Choose Your Project window, choose Phone and Tablet > Empty Activity, and then click Next.
  3. On the Configure Your Project window, enter the following content:
    • Name: Android project name, for example, HelloWord
    • Package name: project package name, for example, com.jocloud.helloword
    • Project location: project saving path
    • Language: select the programing language, for example, Java
    • Minimum API level: the lowest API level in the project

3.2 Step 2: Add Library Files

When integrating, add URL source to Project build.gradle:

buildscript {
    repositories {
        google()
        jcenter()

        [Change the domain name of warehouse address todo]
        maven {url "http://nexus.jocloud.com:8081/nexus/content/groups/public/" }
    }
}

allprojects {
    repositories {
        google()
        jcenter()

        [Change the domain name of warehouse address todo]
        maven {url "http://nexus.jocloud.com:8081/nexus/content/groups/public/" }
    }
}

Add the following dependencies to module build. gradle:

implementation "com.hummer:hmr:3.1.4"
implementation "com.hummer:rts:3.1.4"

After code obfuscation is enabled, add the following codes to the rule file proguard-rules.pro:

-keep class com.hummer.**{*;}
-keepclassmembers class com.hummer.** {
   public *;
}

4 Basic Operations

This section describes how to use basic functions provided by an SDK.

4.1 Initialize and Log In

4.1.1 Step 1: Initialize a Hummer SDK

Before calling Hummer API, you should initialize a Hummer SDK to ensure the initialization of each SDK component and normal operation of the SDK. Call init to initialize the Hummer SDK.

// For details about how to initialize the Hummer SDK and parameters of the running environment, contact us
// appContext Context of Android Activity
// appId AppId issued by the service platform for the app program developer
// listener Event listener
HMR.init(appContext, appId, "", new HMR.HummerEventListener() {
    @Override
    public void onHummerStateChanged(HMR.State fromState, HMR.State toState, String reason) {

    }

    @Override
    public void onHummerKicked(int code, String description) {
                
    }

    @Override
    public void onHummerPreviousTokenExpired() {
                
    }
});

4.1.2 Step 2: Log In an SDK

The SDK only can be used after user login. The service shall call login after initializing the SDK to notify user’s uid and some corresponding information, so the SDK can authenticate users' validity and create their context.

// java
// uid User iD, which cannot be 0
// region Configured region parameter
// token Credential for identifying user's validity
// completion Callback after SDK login succeeded
HMR.login(uid, region, token, new HMR.Completion() {
    @Override
    public void onSuccess(RequestId requestId) {

    }

    @Override
    public void onFailed(RequestId requestId, final Error err) {

    }
});

4.2 Usage of Real-time Signaling Messages

The following figure shows the time sequence of API calling of signaling messages:

Send and Receive P2P Signaling Messages

sequenceDiagram UserA->>SDK: Create a message (new Message) UserA->>SDK: Send a message (PeerService.sendMessage) SDK-->>UserA: completion SDK->>UserB: Receive a message (onPeerMessageReceived) UserB->>SDK: Create a message (new Message) UserB->>SDK: Send a message (PeerService.sendMessage) SDK->>UserB: completion SDK->>UserA: Receive a message (onPeerMessageReceived)

The Hummer SDK provides APIs for sending and receiving P2P signaling messages. Detailed API usage is as follows:

4.2.1 Step 1: Add a Message Listener


//You will receive a callback notification of this event while receiving a P2P signaling message.
// message Received signaling message
// fromUserId Sender

 @Override
 public void onPeerMessageReceived(long fromUserId, Message message) {

 }

4.2.2 Step 2: Send a P2P Signaling Message

The Hummer SDK provides APIs for sending P2P signaling messages. Detailed API usage is as follows:

// java
// Send a P2P signaling message
// message Signaling message to be sent
// options Configuration information for sending a signaling message
// uid Receiver ID of a signaling message
// completion Callback of sending completion
Message message = new Message("10086", text.getBytes(), new HashMap<String, String>(0));

HMR.getService(PeerService.class).sendMessage(Long.valueOf(uid), message, new MessagingOptions(), new HMR.Completion() {
    @Override
    public void onSuccess(RequestId requestId) {

    }

    @Override
    public void onFailed(RequestId requestId, Error err) {
                                                    
    }
});

4.3 Usage of Room APIs

The following figure shows the time sequence of room API calling:

Join or Exit a Room

sequenceDiagram App->>SDK: initialize SDK (init) App->>SDK: log in an SDK (login) SDK-->>App: completion App->>SDK: join a room (join) SDK-->>App: completion App->>SDK: exit a room (leave) SDK-->>App: completion App->>SDK: log out an SDK (logout)

Receive and Send Messages within a Room

sequenceDiagram UserA->>SDK: create a message (new Message) UserA->>SDK: send a message (RoomService.sendMessage) SDK-->>UserA: completion SDK->>UserB: receive a message (onRoomMessageReceived) UserB->>SDK: create a message (new Message) UserB->>SDK: send a message (RoomService.sendMessage) SDK->>UserB: completionHandler SDK->>UserA: receive a message (onRoomMessageReceived)

4.3.1 Step 1: Join a Room

Joining a room by multiple ends is supported. The configuration can be performed by the related background staff. You can call join to join a room. When joining a room, you can import extras to input custom extension information and use options to import optional parameters.

// java
// API for joining a room
// roomId  ID of a target room
// extras  Additional profiles for joining a room, which can be carried for a service when a user joins a room
// options Optional parameters for joining a room
// completion Callback. The service can be processed based on sending results. The failure cause is carried in the callback after the joining failed.
HMR.getService(RoomService.class).join(roomId, new HashMap<String, String>(0), null, new HMR.Completion() {
    @Override
    public void onSuccess(RequestId requestId) {

    }

    @Override
    public void onFailed(RequestId requestId, Error err) {

    }
});

4.3.2 Step 2: Send Messages within a Room

The service can send room messages by calling sendMessage. When creating a message object, you should enter message types to distinguish different messages, input options to carry configuration information, and input roomId for indicating the room where messages are sent.

// java
// Method for sending a room message
// message    Message
// options       Configuration information for sending a message
// roomId        Room ID, indicating a room where a message is to be sent
// completion    Callback of sending a message. The service can be processed by service logic matching sending results.
Message message = new Message("1008611", text.getBytes());

HMR.getService(RoomService.class).sendMessage(roomId, message, new MessagingOptions(), new HMR.Completion() {
    @Override
    public void onSuccess(RequestId requestId) {

    }

    @Override
    public void onFailed(RequestId requestId, Error err) {

    }
});

4.3.3 Step 3: Receive a Message

The service needs to register RoomEventListener to receive room messages. After registration, the service can receive and process message object received in didRoomMessageReceived. This API provides the sender ID and room ID of this message.

// java
// When you sends a message to a room, all members of this room will receive the callback notification of this event.
// message      Received message
// fromUserId   Member ID, indicating a message sender's ID
// roomId       Room ID
@Override
public void onRoomMessageReceived(@NonNull RoomId roomId, long fromUserId, Message message) {

}

4.3.4 Step 4: Exit a Room

After all required logics are processed upon joining a room, you can terminate the interaction with the room by calling leaveRoom, while terminating the listening to signaling messages and callback notification.

// java
// API for leaving a room
// roomId        ID of a room to exit from
// completion  Callback upon completion of leaving a room. The service performs processing based on leaving results. The failure cause is carried in the callback after the leaving failed.
HMR.getService(RoomService.class).leave(roomId, new HMR.Completion() {
    @Override
    public void onSuccess(RequestId requestId) {

    }

    @Override
    public void onFailed(RequestId requestId, Error err) {

    }
});

4.4 Log Out an SDK

Logging out an SDK is to inform the SDK that current users have been logged out, so as to ensure it can receive resources. Log out by calling logout.

// java
// Context of the user attempting to log out
// This operation must be executed before a user deregisters (logs out of) with a service.
HMR.logout();

5. API Reference

This section lists all APIs involved in this document.

6 Notices

Was this page helpful?

Helpful Not helpful
Submitted! Your feedback would help us improve the website.
Feedback
Top
<