io.agora.rtm.RtmMessage Maven / Gradle / Ivy
package io.agora.rtm;
import io.agora.common.internal.CalledByNative;
import io.agora.rtm.RtmConstants.RtmMessageType;
public class RtmMessage {
/**
* String message content.
*/
private String message = "";
/**
* Binary message data.
*/
private byte[] data;
/**
* Message type
*/
private RtmMessageType type;
/**
* Retrieves the payload of the message.
*
* @apiNote If the message payload is a string(you can get message type by
* {@code getType()}), will return a string. If the message payload is
* binary, will return a byte array.
*/
public Object getData() {
if (this.type == RtmMessageType.STRING) {
return this.message;
} else if (this.type == RtmMessageType.BINARY) {
return this.data;
} else {
return null;
}
}
/**
* Retrieves the type of the message.
*
* @return The type of the message, see {@link RtmMessageType}
*/
public RtmMessageType getType() {
return this.type;
}
/**
* Creates a new instance of {@code RtmMessage} with default parameters.
*/
public RtmMessage() {
this.message = "";
this.data = null;
this.type = RtmMessageType.BINARY;
}
@CalledByNative
public RtmMessage(byte[] data, int type) {
this.type = RtmMessageType.getEnum(type);
if (this.type == RtmMessageType.STRING) {
this.message = new String(data);
} else {
this.data = data;
}
}
@Override
public String toString() {
return "RtmMessage {message: " + message + ", data: " + data + ", type: " + type + "}";
}
}