XMPP系列(3)-获取好友列表、添加好友

XMPP系列(三)---获取好友列表、添加好友

1、心跳检测、掉线重连功能

客户端和服务器端都可以设置多久发送一次心跳包,如果对方没有返回正确的pong信息,则会断开连接,而添加掉线重连功能,则会自动进行连接。

如果自己写聊天功能还得自己做心跳检测和掉线重连,比较麻烦。好在XMPP中已经做了心跳检测和掉线重连的模块,只需要几行代码加进去就能实现掉线重连,非常方便。

XMPP中Extensions文件下都是可以自己添加的Module,它们都继承自XMPPModule,而添加的方法也非常的简单:

<span style="font-size:18px;">//添加功能模块
        //1.autoPing 发送的时一个stream:ping 对方如果想表示自己是活跃的,应该返回一个pong
        _xmppAutoPing = [[XMPPAutoPing alloc] init];
        //所有的Module模块,都要激活active
        [_xmppAutoPing activate:self.xmppStream];
        
        //autoPing由于他回定时发送ping,要求对方返回pong,因此这个时间我们需要设置
        [_xmppAutoPing setPingInterval:1000];
        //不仅仅是服务器来得响应;如果是普通的用户,一样会响应
        [_xmppAutoPing setRespondsToQueries:YES];
        //这个过程是C---->S  ;观察 S--->C(需要在服务器设置)
        
        //2.autoReconnect 自动重连,当我们被断开了,自动重新连接上去,并且将上一次的信息自动加上去
        _xmppReconnect = [[XMPPReconnect alloc] init];
        [_xmppReconnect activate:self.xmppStream];
        [_xmppReconnect setAutoReconnect:YES];</span>

2.获取好友列表

获取好友列表和好友操作都需要用到好友模块,首先添加好友模块:

// 3.好友模块 支持我们管理、同步、申请、删除好友
        _xmppRosterMemoryStorage = [[XMPPRosterMemoryStorage alloc] init];
        _xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_xmppRosterMemoryStorage];
        [_xmppRoster activate:self.xmppStream];
        
        //同时给_xmppRosterMemoryStorage 和 _xmppRoster都添加了代理
        [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
        //设置好友同步策略,XMPP一旦连接成功,同步好友到本地
        [_xmppRoster setAutoFetchRoster:YES]; //自动同步,从服务器取出好友
        //关掉自动接收好友请求,默认开启自动同意
        [_xmppRoster setAutoAcceptKnownPresenceSubscriptionRequests:NO];

然后实现好友同步的代理方法:

/**
 * 同步结束
 **/
//收到好友列表IQ会进入的方法,并且已经存入我的存储器
- (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kXMPP_ROSTER_CHANGE object:nil];
}

然后在好友列表控制取出好友数据

#pragma mark - notification event
- (void)rosterChange
{
    //从存储器中取出我得好友数组,更新数据源
    self.contacts = [NSMutableArray arrayWithArray:[JKXMPPTool sharedInstance].xmppRosterMemoryStorage.unsortedUsers];
    [self.tableView reloadData];
    
}

关于好友列表界面的cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"contactCell" forIndexPath:indexPath];
    
    XMPPUserMemoryStorageObject *user = self.contacts[indexPath.row];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:1001];
    nameLabel.text = user.jid.user;
    
    UILabel *statusLabel = (UILabel *)[cell viewWithTag:1002];
    if ([user isOnline]) {
        statusLabel.text = @"[在线]";
        statusLabel.textColor = [UIColor blackColor];
        nameLabel.textColor = [UIColor blackColor];
    } else {
        statusLabel.text = @"[离线]";
        statusLabel.textColor = [UIColor grayColor];
        nameLabel.textColor = [UIColor grayColor];
    }
    
    return cell;
}
3、添加好友功能

首先在JKXMPPTool中添加一个方法:

- (void)addFriend:(XMPPJID *)aJID
{
    //这里的nickname是我对它的备注,并非他得个人资料中得nickname
    [self.xmppRoster addUser:aJID withNickname:@"好友"];
}

然后实现好友模块代理方法:

// 如果不是初始化同步来的roster,那么会自动存入我的好友存储器
- (void)xmppRosterDidChange:(XMPPRosterMemoryStorage *)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kXMPP_ROSTER_CHANGE object:nil];
}

收到好友请求时的处理:

/** 收到出席订阅请求(代表对方想添加自己为好友) */
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence
{
     NSLog(@"didReceivePresenceSubscriptionRequest");
    //添加好友一定会订阅对方,但是接受订阅不一定要添加对方为好友
    self.receivePresence = presence;
    
    NSString *message = [NSString stringWithFormat:@"【%@】想加你为好友",presence.from.bare];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"拒绝" otherButtonTitles:@"同意", nil];
    [alertView show];
    
    //同意并添加对方为好友
//    [self.xmppRoster acceptPresenceSubscriptionRequestFrom:presence.from andAddToRoster:YES];
    //拒绝的方法
//    [self.xmppRoster rejectPresenceSubscriptionRequestFrom:presence.from];
}

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    //收到对方取消定阅我得消息
    if ([presence.type isEqualToString:@"unsubscribe"]) {
        //从我的本地通讯录中将他移除
        [self.xmppRoster removeUser:presence.from];
    }
}

demo地址:https://github.com/Joker-King/ChatDemo


版权声明:本文为博主原创文章,未经博主允许不得转载。