融云im封装

#import 

NS_ASSUME_NONNULL_BEGIN

@interface TYWJRongCloudTool : NSObject
+ (instancetype)sharedTool;
//登陆融云服务器
- (void)connectWithToken;
//加入融云服务器
- (void)joinChatRoom:(NSString *)roomId;
//退出融云服务器
- (void)quitChatRoom:(NSString *)roomId;
//获取会话列表
- (NSArray *)getConversationList;
@end

NS_ASSUME_NONNULL_END
#import "TYWJRongCloudTool.h"
#import 
static TYWJRongCloudTool *_instance = nil;
@interface TYWJRongCloudTool ()
@end
@implementation TYWJRongCloudTool
+ (instancetype)sharedTool {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[TYWJRongCloudTool alloc] init];
    });
    return _instance;
}
- (void)connectWithToken{
    NSString *token = [ZLUserDefaults objectForKey:@"rongyun_token"];
    if ([TYWJCommonTool isBlankString:token]) {
        return;
    }
    [[RCIMClient sharedRCIMClient] connectWithToken:token
                                           dbOpened:^(RCDBErrorCode code) {
    }
                                            success:^(NSString *userId) {
        NSArray *arr = [self getConversationList];
        NSLog(@"融云=======登陆成功");
    }
                                              error:^(RCConnectErrorCode status) {
        NSLog(@"融云=======登陆失败");
    }];
    [[RCIMClient sharedRCIMClient] setReceiveMessageDelegate:self object:nil];
}
- (void)joinChatRoom:(NSString *)roomId{
    [[RCIMClient sharedRCIMClient] joinChatRoom:roomId
                                   messageCount:20
                                        success:^{
        NSLog(@"融云=======进入聊天室成功");
    }
                                          error:^(RCErrorCode status) {
        NSLog(@"融云=======进入聊天室失败");
    }];
}
- (void)quitChatRoom:(NSString *)roomId{
    [[RCIMClient sharedRCIMClient] quitChatRoom:roomId success:^{
        NSLog(@"融云=======退出聊天室成功");
    } error:^(RCErrorCode status) {
        NSLog(@"融云=======退出聊天室失败");
    }];
}
- (void)onReceived:(RCMessage *)message
              left:(int)nLeft
            object:(id)object {
    if ([message.content isMemberOfClass:[RCTextMessage class]]) {
        RCTextMessage *testMessage = (RCTextMessage *)message.content;
        NSLog(@"融云=======消息内容:%@", testMessage.content);
        [ZLNotiCenter postNotificationName:TYWJReceiveCarLocationNoti object:testMessage.content];
    }
    NSLog(@"还剩余的未接收的消息数:%d", nLeft);
}
- (NSArray *)getConversationList{
    return [[RCIMClient sharedRCIMClient] getConversationList:@[@(ConversationType_PRIVATE)]];
}
@end

你可能感兴趣的:(融云im封装)