iOS XMPP研究探索:添加好友

公开一个API,提供添加好友功能:

- (void)addBuddyWithJid:(NSString *)jidString completion:(HYBCompletionBlock)completion {
  if (![jidString hasSuffix:kServer]) {
    jidString = [NSString stringWithFormat:@"%@@%@", jidString, kServer];
  }
  
  // 先判断是否已经是我的好友,如果是,就不再添加
  if ([_xmppRosterStorage userForJID:[XMPPJID jidWithString:jidString]
                          xmppStream:_xmppStream
                managedObjectContext:[self rosterContext]]) {
    if (completion) {
      completion(NO, [NSString stringWithFormat:@"%@已经是您的好友!", jidString]);
    }
    return;
  }
  
  self.completionBlock = completion;
  
  // 设置服务器
  [_xmppStream setHostName:kServer];
  
// 发送添加好友请求
  /*
   presence.type有以下几种状态:
   
   available: 表示处于在线状态(通知好友在线)
   unavailable: 表示处于离线状态(通知好友下线)
   subscribe: 表示发出添加好友的申请(添加好友请求)
   unsubscribe: 表示发出删除好友的申请(删除好友请求)
   unsubscribed: 表示拒绝添加对方为好友(拒绝添加对方为好友)
   error: 表示presence信息报中包含了一个错误消息。(出错)
   */
  [_xmppRoster subscribePresenceToUser:[XMPPJID jidWithString:jidString]];
}

注意:这里添加了是否已经是好友的判断,

[_xmppRosterStorage userForJID:[XMPPJID jidWithString:jidString]
                          xmppStream:_xmppStream
                managedObjectContext:[self rosterContext]])

这个方法是判断用户jid是否已经存在,如果存在,表示已经是我的好友了,如果为nil,表示不存在我的好友列表中,

则可以发送好友添加请求。

节点presence的类型type可以有多种值:

   available: 表示处于在线状态(通知好友在线)
   unavailable: 表示处于离线状态(通知好友下线)
   subscribe: 表示发出添加好友的申请(添加好友请求)
   unsubscribe: 表示发出删除好友的申请(删除好友请求)
   unsubscribed: 表示拒绝添加对方为好友(拒绝添加对方为好友)
   error: 表示presence信息报中包含了一个错误消息。(出错)


发送后, 会在代理函数中调用:

// 加好友回调函数
/*
 presence.type有以下几种状态:
 
 available: 表示处于在线状态(通知好友在线)
 unavailable: 表示处于离线状态(通知好友下线)
 subscribe: 表示发出添加好友的申请(添加好友请求)
 unsubscribe: 表示发出删除好友的申请(删除好友请求)
 unsubscribed: 表示拒绝添加对方为好友(拒绝添加对方为好友)
 error: 表示presence信息报中包含了一个错误消息。(出错)
 */
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence {
  NSLog(@"接收到好友申请消息:%@", [presence fromStr]);
  // 好友在线状态
  NSString *type = [presence type];
  // 发送请求者
  NSString *fromUser = [[presence from] user];
  // 接收者id
  NSString *user = _xmppStream.myJID.user;
  
  NSLog(@"接收到好友请求状态:%@   发送者:%@  接收者:%@", type, fromUser, user);
  
  // 防止自己添加自己为好友
  if (![fromUser isEqualToString:user]) {
    if ([type isEqualToString:@"subscribe"]) { // 添加好友
      // 接受添加好友请求,发送type=@"subscribed"表示已经同意添加好友请求并添加到好友花名册中
      [_xmppRoster acceptPresenceSubscriptionRequestFrom:[XMPPJID jidWithString:fromUser]
                                          andAddToRoster:YES];
      NSLog(@"已经添加对方为好友,这里就没有弹出让用户选择是否同意,自动同意了");
    } else if ([type isEqualToString:@"unsubscribe"]) { // 请求删除好友
      
    }
  }
}

// 添加好友同意后,会进入到此代理
- (void)xmppRoster:(XMPPRoster *)sender didReceiveRosterPush:(XMPPIQ *)iq {
  NSLog(@"添加成功!!!didReceiveRosterPush -> :%@",iq.description);
  
  DDXMLElement *query = [iq elementsForName:@"query"][0];
  DDXMLElement *item = [query elementsForName:@"item"][0];
  
  NSString *subscription = [[item attributeForName:@"subscription"] stringValue];
  // 对方请求添加我为好友且我已同意
  if ([subscription isEqualToString:@"from"]) {// 对方关注我
    NSLog(@"我已同意对方添加我为好友的请求");
  }
  // 我成功添加对方为好友
  else if ([subscription isEqualToString:@"to"]) {// 我关注对方
    NSLog(@"我成功添加对方为好友,即对方已经同意我添加好友的请求");
  } else if ([subscription isEqualToString:@"remove"]) {
    // 删除好友
    if (self.completionBlock) {
      self.completionBlock(YES, nil);
    }
  }
}

/**
 * Sent when the roster receives a roster item.
 *
 * Example:
 *
 * <item jid='romeo@example.net' name='Romeo' subscription='both'>
 *   <group>Friends</group>
 * </item>
 **/
// 已经互为好友以后,会回调此
- (void)xmppRoster:(XMPPRoster *)sender didReceiveRosterItem:(NSXMLElement *)item {
  NSString *subscription = [item attributeStringValueForName:@"subscription"];
  if ([subscription isEqualToString:@"both"]) {
    NSLog(@"双方已经互为好友");
    if (self.buddyListBlock) {
      // 更新好友列表
      [self fetchBuddyListWithCompletion:self.buddyListBlock];
    }
  }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取好友列表和加入好友都是 XMPP 协议中常见的操作,可以使用 XMPP 客户端库来实现。 获取好友列表: 获取好友列表需要先建立与服务器的连接,然后向服务器发送获取好友列表的请求,服务器会返回当前用户的好友列表。 在使用 XMPP 客户端库时,可以通过调用 `getRoster()` 方法来获取好友列表: ```java Roster roster = connection.getRoster(); Collection<RosterEntry> entries = roster.getEntries(); for (RosterEntry entry : entries) { System.out.println(entry.getName() + " (" + entry.getUser() + ")"); } ``` 这段代码会输出当前用户的好友列表,每个好友的名称和 JID。 加入好友: 加入好友需要知道好友的 JID,然后向服务器发送加入好友的请求,服务器会向好友发送一个订阅请求,好友同意后就成为了当前用户的好友。 在使用 XMPP 客户端库时,可以通过调用 `sendPacket()` 方法来发送加入好友的请求: ```java Presence subscribe = new Presence(Presence.Type.subscribe); subscribe.setTo(buddyJID); connection.sendPacket(subscribe); ``` 这段代码会向好友发送一个订阅请求,其中 `buddyJID` 是好友的 JID。 好友同意后,当前用户会收到一个订阅通知,可以通过监听 `RosterListener` 的 `entriesAdded()` 方法来处理: ```java roster.addRosterListener(new RosterListener() { public void entriesAdded(Collection<String> addresses) { for (String address : addresses) { System.out.println("New buddy added: " + address); } } // ... }); ``` 这段代码会在当前用户的好友列表中添加新的好友,并输出好友的 JID。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值