2

在我的聊天应用程序中,我想获取所有在线注册用户。所以每个人,而不仅仅是我名单中的人,这是通过以下代码实现的:

- (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"]];
    }
}
}

使用此代码,用户只能看到“朋友”的其他用户,但我需要在此特定域上注册的所有用户。ejabberd可以做到这一点吗?

4

3 回答 3

4
- (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];
        }



    }
于 2013-08-20T17:48:49.220 回答
2

我有同样的问题,我也遇到queryElementnil。我更改了响应代码以查看如下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",这给了我用户列表。

我正在使用 ejabberd,不确定这是否适用于所有其他 XMPP 服务器。

另外我发现这给了我“好友”用户的列表,看起来如果你想要“所有”用户,你需要以管理员用户身份进行查询。请查看此链接以获取更多信息:https ://www.ejabberd.im/node/3420

于 2015-01-19T06:05:49.337 回答
0

谷歌搜索后,您无法轻松获得所有用户,您必须按照示例 1 中的步骤创建共享名册组:每个人都可以看到其他人完成此操作后,您将通过以下委托方法获得所有在线用户。

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
于 2016-08-23T14:04:28.833 回答