# 消息通道
透明通道消息主要包含房间内消息(接口集成使用请查看章节1)和在线消息(接口集成使用请查看章节2),具体使用要求和场景举例参考下表:
在线消息 | 房间内消息 | |
---|---|---|
一对一发送 | 支持 | 支持 |
群发消息 | 不支持 | 支持 |
是否支持异步发送结果上报 | 支持 | 不支持 |
是否需要登录 | 是 | 是 |
是否需要建立通话 | 否 | 是 |
使用场景举例 | 1、实现不依赖通话的一对一聊天;2、实现一些通话前的自定义信令交互,比如呼叫、拒接/接听等等; | 1、实现通话中的一些自定义信令、通知等;2、实现通话中单聊和群聊; |
消息内容支持 | 只支持文本消息 | 只支持文本消息 |
消息内容大小最大支持(bit) | 4K | 4K |
# 1. 房间内消息
如果想在房间内给其他成员发送消息,可以调用 sendMessage (opens new window) 接口:
/**
* 发送房间消息,消息内容不能大于4K
*
* 消息接收方都会收到 {@link JRTCRoomCallback#onMessageReceived onMessageReceived} 回调
* @param type 消息类型
* @param content 消息内容
* @param toUserId 指定成员的用户ID,传 null 即给通话中全部成员发送消息
* @return 接口调用结果
* - true: 接口调用成功
* - false: 接口调用异常
*/
public abstract boolean sendMessage(String type, String content, String toUserId);
消息通过实现 JRTCRoomCallback (opens new window) 的 onMessageReceived (opens new window) 接口上报。
/**
* 接收房间消息的回调
* <p>
* 当房间中有成员调用 {@link JRTCRoom#sendMessage sendMessage} 接口发送消息时,接收消息的成员会收到此回调。
*
* @param type 消息类型,对应 {@link JRTCRoom#sendMessage sendMessage} 方法中的 type 参数
* @param content 消息内容,对应 {@link JRTCRoom#sendMessage sendMessage} 方法中的 content 参数
* @param fromUserId 消息发送成员的用户ID
* @param room 当前 JRTCRoom 对象
*/
void onMessageReceived(String type, String content, String fromUserId, JRTCRoom room);
示例代码:
//成员1发送消息给成员2
this.room.sendMessage("content", "contentType",messageType, "成员2");
//成员1发送消息给房间内所有成员
this.room.sendMessage("content", "contentType",messageType, null);
//成员2接收消息
public void onMessageReceived(String type, String content, String fromUserId, JRTCRoom room) {
if (room == this.room) {
//成员2接收到来自成员1发送的消息
}
}
# 2.在线消息
只要登录到 Juphoon RTC 平台就可以通过 JRTCClient (opens new window) 的 sendOnlineMessage (opens new window) 实现在线消息的收发,消息内容不能大于4K。
/**
* 发送在线消息
*
* @param message 消息内容
* @param userId 对端的用户名
* @return 接口调用结果
* - 操作id: 接口调用成功,对应 {@link JRTCClientCallback#onOnlineMessageSendResult onOnlineMessageSendResult} 回调的 operatorId 参数
* - -1: 接口调用异常,不会收到回调
*/
public abstract int sendOnlineMessage(String message, String userId);
在线消息发送结果通过 onOnlineMessageSendResult (opens new window) 回调通知。
/**
* 在线消息发送结果回调
* @param result 发送结果是否成功
* - true:发送成功
* - false:发送失败
* @param operatorId 操作id,对应 {@link JRTCClient#sendOnlineMessage sendOnlineMessage} 的返回值
*/
void onOnlineMessageSendResult(boolean result, int messageId);
在线消息接收者会收到 onOnlineMessageReceived (opens new window) 回调通知。
/**
* 收到在线消息回调
* @param message 消息内容
* @param userId 对方用户ID
*/
void onOnlineMessageReceived(String message, String userId);
示例代码:
// 给用户 7777 发送在线消息
this.operatorId = client.sendOnlineMessage("消息内容", "777");
// 给用户 7777 发送在线消息结果
public void onOnlineMessageSendResult(boolean result, int operatorId) {
if (this.operatorId == operatorId) {
if(result) {
// 在线消息发送成功
} else {
// 在线消息发送失败
}
}
}
// 收到在线消息
public void onOnlineMessageReceived(String message, String userId) {
// 收到来自 userId 的消息,消息内容为 message
}