XMPP - 名片

名片(vCard) 属于 XMPP 扩展协议 XEP - 0054 部分。

每一个名片模块的实例都包含了一个用户实例的所有身份信息。虽然针对单纯集成 XMPP 实现 IM 功能的应用来说可能并不是很需要用到这部分协议,但是作为纯使用 XMPP 协议开发的应用来说这个模块就显得很有必要了。(举个栗子:Facebook 与 Facebook Messenger)

XMPPFramework 中的 vCard

如果我们想要在 XMPPFramework 中使用 vCard 功能,那么有三个类是必须要接触的:XMPPvCardTempXMPPvCardTempModuleXMPPvCardCoreDataStorage

  • XMPPvCardTemp: vCard 实例模型,包含用户的信息
  • XMPPvCardTempModule: 交互模块,需要加载在 XMPPStream
  • XMPPvCardCoreDataStorage: XMPPvCardTemp 的缓存模块,避免重复请求网络

下面我们直接上代码

// 使用 vCard 模块步骤:
// 1、在初始化 XMPPStream 时启用 vCard 模块
- (void)initalize
{
    // 初始化连接
    _xmppStream = [[XMPPStream alloc] init];
    [_xmppStream setHostName:XMPP_HOST];
    [_xmppStream setHostPort:XMPP_PORT];
    [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppStream setKeepAliveInterval:30];
    [_xmppStream setEnableBackgroundingOnSocket:YES];
    
    // 接入断线重连模块
    _xmppReconnect = [[XMPPReconnect alloc] initWithDispatchQueue:dispatch_get_main_queue()];
    [_xmppReconnect setAutoReconnect:YES];
    [_xmppReconnect activate:_xmppStream];
    
    // 接入流管理模块
    _streamStorage = [[XMPPStreamManagementMemoryStorage alloc] init];
    _xmppStreamManagement = [[XMPPStreamManagement alloc] initWithStorage:_streamStorage];
    [_xmppStreamManagement setAutoResume:YES];
    [_xmppStreamManagement addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppStreamManagement activate:_xmppStream];
    
    // 接入好友模块
    _rosterStorage = [[XMPPRosterMemoryStorage alloc] init];
    _xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_rosterStorage];
    [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppRoster activate:_xmppStream];
    
    // 接入好友名片模块
    _vCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
    _xmppVCard = [[XMPPvCardTempModule alloc] initWithvCardStorage:_vCardStorage];
    [_xmppVCard addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppVCard activate:_xmppStream];
}

// 2、针对好友,在获取到好友列表后匹配对应名片
- (void)xmppRosterDidChange:(XMPPRosterMemoryStorage *)sender
{
    // 获取自己的名片
    _myVCard = [_xmppVCard myvCardTemp];
    // 获取好友名片
    NSArray  *users = [sender unsortedUsers];
    [users enumerateObjectsUsingBlock:^(XMPPUserMemoryStorageObject * _Nonnull user, NSUInteger idx, BOOL * _Nonnull stop)
    {
        // 关键代码,匹配好友名片并存入缓存中
        [_xmppVCard fetchvCardTempForJID:user.jid ignoreStorage:NO];
    }];
    
    // 通知更新花名册
    [[NSNotificationCenter defaultCenter] postNotificationName:XMPPRosterChanged object:nil];
}

// 3、取出缓存中的名片
- (void)getFriendsVCard
{
    // 从数据库中获取成员名片
    _memberVCards = [NSMutableArray array];
    [_members enumerateObjectsUsingBlock:^(IMRoomMemberModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        XMPPJID        *jid = [XMPPJID jidWithString:obj.jid];
        XMPPvCardTemp  *memberVCard = [getXMPPManager().xmppVCard vCardTempForJID:jid shouldFetch:YES];
        if (memberVCard) {
            
            [_memberVCards addObject:memberVCard];
        }
    }];
}

想了想名片也不知道还有什么值得的写的,就这样结束这篇内容好了。如果有问题的欢迎在评论中提出来

你可能感兴趣的:(XMPP - 名片)