via:AVOS Cloud Bloggit
不少开发者想在本身的 App 中添加实时通信的功能,但一般由于没有合适的后端支持,最终没能实现。而 AVOSCloud 与时俱进,给你们带来了但愿。下面就来介绍使用 AVOSCloud 给本身的 App 添加实时通信功能。github
AVOSCloud SDK 从 2.5.9 开始提供了实时通信模块。本文主要基于 iOS SDK 2.6.2.1 实现,假设你已经具备必定的 iOS 开发基础,省略掉非实时通信相关的代码,github 完整代码点此 。后端
此部分只列出了通信相关的代码,省略了一些本地对话和消息保存的代码。完整代码能够查看 github 完整代码网络
首先是 SDK 的初始化,在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 里面添加如下代码完成 SDK 的初始化session
[AVOSCloud setApplicationId:AVOSAppID clientKey:AVOSAppKey];
而后是通信模块的初始化,这里使用一个 CDSessionManager 的单例类来管理。这里有两部分app
- (instancetype)init { if ((self = [super init])) { ... AVSession *session = [[AVSession alloc] init]; session.sessionDelegate = self; session.signatureDelegate = self; _session = session; ... [self commonInit]; } return self; }
这里是在整个运行周期只会运行一次的代码,主要是构造一个 Session。post
- (void)commonInit { ... //打开 session [_session open:[AVUser currentUser].username withPeerIds:nil]; ... while ([rs next]) { //遍历本地保存的对话记录 ... if (type == CDChatRoomTypeSingle) { [peerIds addObject:otherid]; } else if (type == CDChatRoomTypeGroup) { ... //加入到已保存的 group 对话中 AVGroup *group = [_session getGroup:otherid]; group.delegate = self; [group join]; } ... } //加入已保存的我的对话 [_session watchPeers:peerIds]; initialized = YES; }
这里是每次从新登陆后都会运行的代码,主要包括打开 session,恢复对话,这里使用用户名做为 peerId。code
对话就是本身与某一个对象(包括我的或群组)的通信过程,开启一个我的对话对象
- (void)addChatWithPeerId:(NSString *)peerId { BOOL exist = NO; ... if (!exist) { //若是对话已经存在就跳过 [_session watchPeers:@[peerId]]; ... } }
开启一个群组对话,这里包括新建群组和加入已有群组blog
- (AVGroup *)startNewGroup { //新建群组 AVGroup *group = [_session getGroup:nil]; group.delegate = self; [group join]; return group; } - (AVGroup *)joinGroup:(NSString *)groupId { //加入已有群组 BOOL exist = NO; ... if (!exist) { //若是对话已经存在就跳过 AVGroup *group = [_session getGroup:groupId]; group.delegate = self; [group join]; ... } return [_session getGroup:groupId]; }
发送消息给我的
- (void)sendMessage:(NSString *)message toPeerId:(NSString *)peerId { ... [_session sendMessage:payload isTransient:NO toPeerIds:@[peerId]]; ... [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_UPDATED object:nil userInfo:dict]; }
发送消息给群组
- (void)sendMessage:(NSString *)message toGroup:(NSString *)groupId { ... [[_session getGroup:groupId] sendMessage:payload isTransient:NO]; ... [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_UPDATED object:nil userInfo:dict]; }
代码中 notification 用于通知 UI 更新
接收我的消息
- (void)onSessionMessage:(AVSession *)session message:(NSString *)message peerId:(NSString *)peerId { ... BOOL exist = NO; ... if (!exist) { //尚未与该人的对话 [self addChatWithPeerId:peerId]; [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SESSION_UPDATED object:session userInfo:nil]; } [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_UPDATED object:session userInfo:dict]; }
接收群组消息
- (void)session:(AVSession *)session group:(AVGroup *)group didReceiveGroupMessage:(NSString *)message fromPeerId:(NSString *)peerId { ... BOOL exist = NO; ... if (!exist) { //尚未与该群组的对话,你可能在别的客户端加入了此群组,但此客户端尚未建立对话记录 [self joinGroup:group.groupId]; [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SESSION_UPDATED object:session userInfo:nil]; } [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_UPDATED object:session userInfo:dict]; }
- (void)session:(AVSession *)session group:(AVGroup *)group didReceiveGroupEvent:(AVGroupEvent)event memberIds:(NSArray *)memberIds { ... if (event == AVGroupEventSelfJoined) { //接收到本身加入群组成功的事件,新加入的群组在此时才能获取 groupId BOOL exist = NO; ... if (!exist) { ... [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SESSION_UPDATED object:session userInfo:nil]; } } }
代码中 notification 用于通知 UI 更新
本文主要展示了怎么使用 AVOSCloud SDK 实现我的和群组聊天通信功能。