在我的聊天应用程序中,我想获得所有在线注册用户。所以每个人,而不仅仅是我名册上的人,这是用这个代码实现的:
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
// a buddy went offline/online
NSString *presenceType = [presence type]; // online/offline
NSString *myUsername = [[sender myJID] user];
NSString *presenceFromUser = [[presence from] user];
if (![presenceFromUser isEqualToString:myUsername]) {
if ([presenceType isEqualToString:@"available"]) {
[_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
} else if ([presenceType isEqualToString:@"unavailable"]) {
[_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
}
}
}
使用此代码,用户只能看到"朋友"的其他用户,但我需要所有用户在该特定域上注册。这可能与射精有关吗?
- (void)getAllRegisteredUsers {
NSError *error = [[NSError alloc] init];
NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/disco#items' node='all users'/>"
error:&error];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get"
to:[XMPPJID jidWithString:@"DOMAIN"]
elementID:[xmppStream generateUUID] child:query];
[xmppStream sendElement:iq];
}
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
NSXMLElement *queryElement = [iq elementForName: @"query" xmlns: @"http://jabber.org/protocol/disco#items"];
if (queryElement) {
NSArray *itemElements = [queryElement elementsForName: @"item"];
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (int i=0; i<[itemElements count]; i++) {
NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
[mArray addObject:jid];
}
}
我遇到了同样的问题,我得到了queryElement
和nil
。我已经更改了响应代码以查看XML,如下所示:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
//DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [iq elementID]);
//NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"http://jabber.org/protocol/disco#items"];
NSXMLElement *queryElement = [iq elementForName:@"query" xmlns: @"jabber:iq:roster"];
NSLog(@"IQ: %@",iq);
if (queryElement) {
NSArray *itemElements = [queryElement elementsForName: @"item"];
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (int i=0; i<[itemElements count]; i++) {
NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
NSLog(@"%@",jid);
[mArray addObject:jid];
}
}
return NO;
}
正如你可能看到的,我更改的是xmlns:
,从这个xmlns: @"http://jabber.org/protocol/disco#items"
到这个xmlns: @"jabber:iq:roster"
,这给了我用户列表。
我正在使用ejabord,不确定这是否适用于所有其他XMPP服务器。
此外,我发现这给了我"好友"用户的列表,看起来如果你想要"所有"用户,你需要以管理员用户的身份进行查询。有关它的更多信息,请查看此链接:https://www.ejabberd.im/node/3420
谷歌搜索后,您不可能轻易获得所有用户,您必须按照示例1中的步骤创建共享名册组:每个人都可以看到其他人。完成此操作后,您将在以下委托方法中获得所有在线用户。
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence