美文网首页
融云im封装

融云im封装

作者: 五角 | 来源:发表于2020-07-22 18:33 被阅读0次
    #import <Foundation/Foundation.h>
    
    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 <RongIMLib/RongIMLib.h>
    static TYWJRongCloudTool *_instance = nil;
    @interface TYWJRongCloudTool ()<RCIMClientReceiveMessageDelegate>
    @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封装

          本文链接:https://www.haomeiwen.com/subject/oyvtlktx.html