【问题标题】:Add multiple users in XMPP roster在 XMPP 名册中添加多个用户
【发布时间】:2018-11-11 15:28:54
【问题描述】:

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

那么有没有更快的方法可以将多个联系人添加到名册中?

我已经解决了很多类似this的问题,但它们都没有帮助。

使用后端 API 过滤设备联系是一种好方法,还是我应该做其他事情?

【问题讨论】:

  • 我认为后端 API 是一个很好的方法,只需将所有联系人发送到服务器。响应服务器提供过滤器联系人
  • @JigarDarji 感谢您的回复,后端 API 绝对是理想的方法。但是如何将这些用户添加到名册中呢?
  • XMPP SDK中没有提供同时添加多个用户的方法。如果一个一个添加使用需要很长时间,那么您可以将它们添加到后台线程中。
  • 你用的是什么Api?如果您使用的是基于 xml 的客户端,您可以发布该节吗?基本上,一旦用户注册并添加到您的名册中,如果节正确,服务器应在您与服务器同步后自动发送整个名册

标签: ios swift xmpp xmppframework


【解决方案1】:

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. 在服务器端编写一些服务 api 和一些 rabbitmq 服务,它会为一个用户插入多个名册,XMPP 服务器会更新你的名册更新。我希望这个答案会对你有所帮助。

【讨论】:

    【解决方案2】:

    我尝试发送此节:

    <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 个项目元素的数据包视为错误情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2011-04-22
      • 2015-09-21
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2012-12-29
      相关资源
      最近更新 更多