This section describes how to rapidly integrate a Hummer SDK Android version to the project.
Open-source example project
Try Demo
This section describes how to create a project and integrate a Hummer SDK to your project.
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 *;
}
This section describes how to use basic functions provided by an 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() {
}
});
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) {
}
});
The following figure shows the time sequence of API calling of signaling messages:
The Hummer SDK provides APIs for sending and receiving P2P signaling messages. Detailed API usage is as follows:
//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) {
}
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) {
}
});
The following figure shows the time sequence of room API calling:
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) {
}
});
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) {
}
});
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) {
}
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) {
}
});
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();
This section lists all APIs involved in this document.