在XMPP名单中添加多个用户

时间:2018-06-01 13:12:44

标签: ios swift xmpp xmppframework

如何在我的名册中添加多个用户?现在我可以一次添加一个用户。但现在我必须与XMPP同步设备联系。使用后端API我可以过滤掉在应用程序中注册的联系人。现在将它们逐一添加到名册中需要花费太多时间。

那么有更快捷的方式将多个联系人添加到名册中吗?

我已经经历了许多问题,例如this,但他们没有帮助。

过滤设备与后端API联系是一种很好的方法,还是我应该做些什么呢?

2 个答案:

答案 0 :(得分:0)

XMPP是一个完整的基于XML的协议,即使通过库没有一些方法,我们也可以根据需要扩展库。因此,当您说要添加多个名单时,有两种方法可以实现它: 1.在XMPP客户端库或应用程序中添加一些方法,并添加多个名单项,如下所示:

- (void)addUsers:(NSArray<XMPPJID *> *)jids withNickname:(NSArray<NSString *> *)optionalNames groups:(NSArray *)groups {

if (jids == nil) return;
XMPPJID *myJID = xmppStream.myJID;



// Add the buddy to our roster
//
// <iq type="set">
//   <query xmlns="jabber:iq:roster">
//     <item jid="bareJID" name="optionalName">
//      <group>family</group>
//     </item>
//   </query>
// </iq>

XMPPIQ *iq = [XMPPIQ iqWithType:@"set"];

for (int i = 0; i < jids.count; i++) {

    XMPPJID *jid = jids[0];
    if ([myJID isEqualToJID:jid options:XMPPJIDCompareBare])
    {
        // You don't need to add yourself to the roster.
        // XMPP will automatically send you presence from all resources signed in under your username.
        //
        // E.g. If you sign in with robbiehanson@deusty.com/home you'll automatically
        //    receive presence from robbiehanson@deusty.com/work

        XMPPLogInfo(@"%@: %@ - Ignoring request to add myself to my own roster", [self class], THIS_METHOD);
        continue;
    }
    NSXMLElement *item = [NSXMLElement elementWithName:@"item"];
    [item addAttributeWithName:@"jid" stringValue:[jid bare]];
    NSString *optionalName = optionalNames[i];
    if(optionalName)
    {
        [item addAttributeWithName:@"name" stringValue:optionalName];
    }

    for (NSString *group in groups) {
        NSXMLElement *groupElement = [NSXMLElement elementWithName:@"group"];
        [groupElement setStringValue:group];
        [item addChild:groupElement];
    }

    NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
    [query addChild:item];
    [iq addChild:query];
}

[xmppStream sendElement:iq];

}

  1. 在服务器端使用一些rabbitmq服务编写一些服务api,它将为用户插入多个名单,XMPP服务器将更新您有关名册更新的信息。我希望这个答案会对你有所帮助。

答案 1 :(得分:0)

我尝试发送此节:

<iq type="set" id="15-47" to="940588870@localhost">
    <query xmlns="jabber:iq:roster" ver="1116247190">
        <item jid="1234@localhost" name="user1" subscription="both">
            <group>acceptance</group>
        </item>
        <item jid="7663@localhost" name="user2" subscription="both">
            <group>acceptance</group>
        </item>
        <item jid="9876@localhost" name="user3" subscription="both">
            <group>acceptance</group>
        </item>
        <item jid="1111@localhost" name="user4" subscription="both">
            <group>acceptance</group>
        </item>
    </query>
</iq>

我收到了

this error : 00:32:03.163 [Smack Cached Executor] WARN  org.jivesoftware.smack.roster.Roster.handleIQRequest(1739) -  - Ignoring roster push with not exactly one entry. size=4

在进一步检查这一点时,XMPP准则表示,它会将名称空间为“ jabber:IQ:roster”且查询元素中包含1个以上item元素的数据包视为错误情况。

相关问题