消息
收发消息是 RTM 服务的基础功能之一,任何使用 RTM 服务发送的消息将会在 100 ms 以内送达到任何一个在线订阅用户端上,你可以一次只给一个人发消息,也可以一次给多人广播消息,这取决于你的使用方式。
RTM 有 3 种频道类型,分别是 Message Channel、User Channel 和 Stream Channel。3 种频道类型的主要区别如下:
- Message Channel:实时频道,消息通过频道传递,可扩展性强。本地用户可以调用
publish
方法,将channelType
参数设为AgoraRtmChannelTypeMessage
,并将channelName
参数设为频道名称,即可在频道中发送消息。远端用户可以调用subscribeWithChannel
方法订阅频道并接收消息。 - User Channel:向指定用户发送点对点消息。本地用户可以调用
publish
方法,将channelType
参数设为AgoraRtmChannelTypeUser
,并将channelName
参数设为对方的userId
,即可向指定用户发送消息。指定用户可以通过didReceiveMessageEvent
事件通知来接收消息。 - Stream Channel:流传输频道,用户需要先加入频道,然后加入 Topic,消息通过 Topic 传递。本地用户可以调用
publishTopicMessage
方法在 Topic 中发送消息,远端用户可以调用subscribeTopic
方法订阅 Topic 并接收消息。
本文介绍如何在 Message Channel 和 User Channel 中收发消息。
publish
接口描述
你可以直接调用publish
方法向订阅该频道的所有在线用户发送消息。即使没有订阅频道,也能在频道中发送消息。
信息
以下做法可有效提升消息收发的可靠性:
- 消息负载限制在 32 KB 以内,否则发送会失败。
- 向单个频道发送消息的速率上限为 30 QPS。如果发送速率超限,将会有部分消息被丢弃。在满足要求的情况下,速率越低越好。
成功调用该方法后,SDK 会触发 didReceiveMessageEvent
事件通知。订阅了该频道并且开启了事件监听的用户将会收到该事件通知,详见事件监听。
接口方法
你可以通过以下方式调用 publish
[1/2] 和 publish
[2/2] 方法:
Objective-C
// publish[1/2]
- (void) publish:(NSString* _Nonnull)channelName
message:(NSString* _Nonnull)message
option:(AgoraRtmPublishOptions* _Nullable)publishOption
completion:(AgoraRtmOperationBlock _Nullable)completionBlock
Objective-C
// publish[2/2]
- (void) publish:(NSString* _Nonnull)channelName
data:(NSData* _Nonnull)data
option:(AgoraRtmPublishOptions* _Nullable)publishOption
completion:(AgoraRtmOperationBlock _Nullable)completionBlock
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelName | NSString | 必填 | - |
|
message | NSString | 必填 | - | 在 publish [1/2] 方法中发布 String 型消息负载。 |
data | NSData | 必填 | - | 在 publish [2/2] 方法中发布 Byte 型消息负载。 |
publishOption | AgoraRtmPublishOptions | 选填 | - | 消息选项。 |
completionBlock | AgoraRtmOperationBlock | 选填 | - | 调用结果回调:
|
AgoraRtmPublishOptions
数据类型包含以下属性:
属性 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
channelType | AgoraRtmChannelType | 必填 | - | 频道类型。详见 AgoraRtmChannelType 。 |
customType | NSString | 必填 | - | 用户自定义字段。仅支持 String 型。 |
基本用法
示例 1:向指定 Message Channel 发送 String 消息
Objective-C
NSString* message = @"Hello Agora!";
NSString* channel = @"your_channel";
[rtm publish:channel message:message option:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
示例 2:向指定 Message Channel 发送 Byte 消息
Objective-C
unsigned char byte_array[5] = {0,1,2,3,4};
NSData* raw_message = [[NSData alloc] initWithBytes:byte_array length:5];
NSString* channel = @"your_channel";
[rtm publish:channel data:raw_message option:nil completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
示例 3:向指定 User Channel 发送 String 消息
Objective-C
NSString* message = @"Hello Agora!";
NSString* user = @"Tony";
AgoraRtmPublishOptions* publish_option = [[AgoraRtmPublishOptions alloc] init];
publish_option.channelType = AgoraRtmChannelTypeUser;
[rtm publish:user message:message option:publish_option completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
示例 4:向指定 User Channel 发送 Byte 消息
Objective-C
NSData* raw_message = [[NSData alloc] initWithBytes:byte_array length:5];
NSString* user = @"Tony";
AgoraRtmPublishOptions* publish_option = [[AgoraRtmPublishOptions alloc] init];
publish_option.channelType = AgoraRtmChannelTypeUser;
[rtm publish:user data:raw_message option:publish_option completion:^(AgoraRtmCommonResponse * _Nullable response, AgoraRtmErrorInfo * _Nullable errorInfo) {
if (errorInfo == nil) {
NSLog(@"publish success!!");
} else {
NSLog(@"publish failed, errorCode %d, reason %@", errorInfo.errorCode, errorInfo.reason);
}
}];
信息
成功调用该方法后,SDK 会触发 didReceiveMessageEvent
事件通知。订阅了该频道并且开启了事件监听的用户将会收到该事件通知,详见事件监听。
接收消息
RTM 提供消息、状态、事件变更等信息的事件通知。通过设置事件监听,你可以接收已订阅频道中的消息和事件。以接收 User Channel 中的消息为例,示例代码如下:
- 在初始化时添加事件监听:
Objective-C
@interface RtmHandler : NSObject <AgoraRtmClientDelegate>
@end
@implementation RtmHandler
// triggered when received message from remote users
-(void) rtmKit:(AgoraRtmClientKit *)rtmKit didReceiveMessageEvent:(AgoraRtmMessageEvent *)event {
NSString* channel = event.channelName;
AgoraRtmChannelType channel_type = event.channelType;
if (channel_type == AgoraRtmChannelTypeUser) {
// process user message
}
}
@end
AgoraRtmClientConfig* rtm_cfg = [[AgoraRtmClientConfig alloc] initWithAppId:@"your_appid" userId:@"your_userid"];
RtmListener* handler = [[RtmListener alloc] init];
NSError* initError = nil;
AgoraRtmClientKit* rtm = [[AgoraRtmClientKit alloc] initWithConfig:rtm_cfg delegate:handler error:&initError];
- 在任意时间添加事件监听:
Objective-C
@interface RtmHandler : NSObject <AgoraRtmClientDelegate>
@end
@implementation RtmHandler
// triggered when received message from remote users
-(void) rtmKit:(AgoraRtmClientKit *)rtmKit didReceiveMessageEvent:(AgoraRtmMessageEvent *)event {
NSString* channel = event.channelName;
AgoraRtmChannelType channel_type = event.channelType;
if (channel_type == AgoraRtmChannelTypeUser) {
// process user message
}
}
@end
RtmListener* handler = [[RtmListener alloc] init];
[rtm addDelegate:handler];
关于如何添加和设置事件监听,详见事件监听章节。