【问题标题】:XMPPFramework - Retrieving Openfire Message ArchivesXMPPFramework - 检索 Openfire 消息档案
【发布时间】:2012-12-19 20:13:08
【问题描述】:

花了几个小时试图解决这个问题,但我很难过!

试图在我的 OpenFire 服务器上获取 2 个用户之间的聊天历史记录,我读到需要我的插件来执行此操作。

所以,我在我的 OpenFire 服务器上安装了“Open Archive”插件并发送以下 XML(根据 XMPP-0136 协议文档):

<iq type="get" id="page1">
   <retrieve xmlns="urn:xmpp:archive" with="username@server.com" start="1469-07-21T02:56:15Z">
      <set xmlns="http://jabber.org/protocol/rsm">
         <max>100</max>
      </set>
   </retrieve>
</iq>

在代码中,这是通过以下方式实现的:

NSXMLElement *iQ = [NSXMLElement elementWithName:@"iq"];
[iQ addAttributeWithName:@"type" stringValue:@"get"];
[iQ addAttributeWithName:@"id" stringValue:@"page1"];

NSXMLElement *retrieve = [NSXMLElement elementWithName:@"retrieve"];
[retrieve addAttributeWithName:@"xmlns" stringValue:@"urn:xmpp:archive"];
[retrieve addAttributeWithName:@"with" stringValue:@"username@server.com"];
[retrieve addAttributeWithName:@"start" stringValue:@"1469-07-21T02:56:15Z"];

NSXMLElement *set = [NSXMLElement elementWithName:@"set"];
[set addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/rsm"];
NSXMLElement *max = [NSXMLElement elementWithName:@"max"];
max.stringValue = @"100";
[set addChild:max];

[retrieve addChild:set];
[iQ addChild:retrieve];

[[[self appDelegate] xmppStream] sendElement:iQ];

返回以下错误:

<iq xmlns="jabber:client" type="error" id="page1" to="username@server.com">
   <error code="404" type="cancel">
      <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
   </error>
</iq>

我的 Xcode 项目可以成功地向我尝试接收聊天记录的用户发送/接收消息,所以我真的不知道我做错了什么。此外,该插件使我能够搜索聊天消息(通过 OpenFire 管理员)并获得成功的结果,因此它似乎可以正常工作并存储消息。

任何帮助将不胜感激。谢谢!

【问题讨论】:

标签: ios objective-c xmpp openfire xmppframework


【解决方案1】:

如果您正在查找聊天记录,我认为您必须将消息保存到核心数据并从那里检索它们。要使用 XMPPFramework 内置功能保存数据,您必须使用以下代码:

XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage   sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];

xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage];
[xmppMessageArchivingModule activate:xmppStream];
[xmppMessageArchivingModule  addDelegate:self delegateQueue:dispatch_get_main_queue()];

现在您必须通过以下方式从核心数据中检索该消息:

-(void)loadarchivemsg
{  
    XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
    NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                      inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];

    NSString *predicateFrmt = @"bareJidStr like %@ ";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFrmt, chatWithUser];
    request.predicate = predicate;
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"kXMPPmyJID"]);
    [request setEntity:entityDescription];
    NSError *error;
    NSArray *messages_arc = [moc executeFetchRequest:request error:&error];

    [self print:[[NSMutableArray alloc]initWithArray:messages_arc]];
}

-(void)print:(NSMutableArray*)messages_arc{
    @autoreleasepool {
        for (XMPPMessageArchiving_Message_CoreDataObject *message in messages_arc) {
            NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
            NSLog(@"to param is %@",[element attributeStringValueForName:@"to"]);

            NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
            [m setObject:message.body forKey:@"msg"];

            if ([[element attributeStringValueForName:@"to"] isEqualToString:chatWithUser]) {               
                [m setObject:@"you" forKey:@"sender"];
            }
            else {
                [m setObject:chatWithUser forKey:@"sender"];
            }

            [messages addObject:m];

            NSLog(@"bareJid param is %@",message.bareJid);
            NSLog(@"bareJidStr param is %@",message.bareJidStr);
            NSLog(@"body param is %@",message.body);
            NSLog(@"timestamp param is %@",message.timestamp);
            NSLog(@"outgoing param is %d",[message.outgoing intValue]);
            NSLog(@"***************************************************");
        }
    }
}

【讨论】:

  • 我没有得到一点我们应该在哪里给其他用户 JID。你能用评论显示特定的地方吗?
  • [NSPredicate predicateWithFormat:predicateFrmt, chatWithUser];这里 chatWithUser 是其他用户 JID 你可以在那里设置其他用户 JID
  • 是的,我知道了,我遇到了其他问题,我正在使用您的代码,但是当我重新获取代码时,我得到了聊天历史记录的最后两条消息,但我没有得到完整的历史记录。Plz给我溶胶。
  • @Romance 我正在使用相同的代码,并且我得到了所有 JID 之间的消息,检查两次(检查你的 sqlite 数据并比较两者)它工作完美。
  • @Mohan 如果用户离线,则所有消息都存储在服务器中,当用户在线时,所有消息都作为延迟消息发送给用户。所以你必须处理这些消息......我希望你能理解
【解决方案2】:

当您在请求中提及开始标签时,它会与具有确切时间戳的聊天匹配,这就是它返回错误代码“404”或“500”的原因。我从我的请求中省略了开始标记并编写了以下代码,该代码返回与用户的整个聊天历史记录。

NSXMLElement *iq1 = [NSXMLElement elementWithName:@"iq"];
[iq1 addAttributeWithName:@"type" stringValue:@"get"];
[iq1 addAttributeWithName:@"id" stringValue:@"pk1"];

NSXMLElement *retrieve = [NSXMLElement elementWithName:@"retrieve" xmlns:@"urn:xmpp:archive"];

[retrieve addAttributeWithName:@"with" stringValue:@"rahul@vishals-mac-pro.local"];
NSXMLElement *set = [NSXMLElement elementWithName:@"set" xmlns:@"http://jabber.org/protocol/rsm"];
NSXMLElement *max = [NSXMLElement elementWithName:@"max" stringValue:@"100"];

[iq1 addChild:retrieve];
[retrieve addChild:set];
[set addChild:max];
[[[self appDelegate] xmppStream] sendElement:iq1]; 

这将返回用户 Rahul 和当前登录用户之间的 XML 响应中的整个聊天历史记录。

更多详细信息请参考此博客http://question.ikende.com/question/363439343236313430

【讨论】:

    【解决方案3】:

    请在https://stackoverflow.com/a/29097289/2225439获取详细的节详细信息

    它是独立于平台的,您只需要了解 Stanza 的结构,并且可以根据您使用的库来创建。

    这是您需要发送以获取存档消息的一系列节。有关更多详细信息,您可以查看 XEP 0136 (http://xmpp.org/extensions/xep-0136.html#manual)

    请求

    <iq type='get' id='mrug_sender@staging.openfire.com'>
           <list xmlns='urn:xmpp:archive'
                   with='mrug_target_155@staging.openfire.com'>
            <set xmlns='http://jabber.org/protocol/rsm'>
                <max>6900</max>
            </set>
          </list>
       </iq>
    

    回复

    <iq type="result" id="mrug_sender@staging.openfire.com" to="mrug_sender@staging.openfire.com/Psi">
    <list xmlns="urn:xmpp:archive">
    <chat with="mrug_target_155@staging.openfire.com" start="2014-06-07T06:52:26.041Z"/>
    <chat with="mrug_target_155@staging.openfire.com" start="2014-06-07T07:06:53.372Z"/>
    <set xmlns="http://jabber.org/protocol/rsm">
    <first index="0">866</first>
    <last>867</last>
    <count>2</count>
    </set>
    </list>
    </iq>
    

    请求

    <iq type='get' id='mrug_sender@staging.openfire.com'>
        <retrieve xmlns='urn:xmpp:archive'  with='mrug_target_155@staging.openfire.com'  start='2014-06-07T06:52:26.041Z'>
         <set xmlns='http://jabber.org/protocol/rsm'>
           <max>8000</max>
         </set>
        </retrieve>
     </iq>
    

    回复

    <iq type="result" id="mrug_sender@staging.openfire.com" to="mrug_sender@staging.openfire.com/Psi">
    <chat xmlns="urn:xmpp:archive" with="mrug_target_155@staging.openfire.com" start="2014-06-07T06:52:26.041Z">
    <from secs="0" jid="mrug_target_155@staging.openfire.com">
    <body>Hello This is Cool</body>
    </from>
    <set xmlns="http://jabber.org/protocol/rsm">
    <first index="0">0</first>
    <last>0</last>
    <count>1</count>
    </set>
    </chat>
    </iq>
    

    获取所有对话列表

    <iq type='get' id='mrug_sender@staging.openfire.com'>
           <list xmlns='urn:xmpp:archive'>
            <set xmlns='http://jabber.org/protocol/rsm'>
                <max>6900</max>
            </set>
          </list>
    </iq>
    

    【讨论】:

      【解决方案4】:

      XMPPFramework 实现了 XEP-0136。您是否尝试过使用XMPPMessageArchiving 设置首选项或将服务器的存档同步到客户端?

      【讨论】:

      • 嗨@noa!您能否分享一个示例,说明如何设置存档首选项并将服务器存档同步到客户端。
      猜你喜欢
      • 2012-07-08
      • 2018-05-10
      • 1970-01-01
      • 2013-10-27
      • 2016-12-26
      • 2016-12-09
      • 2012-01-03
      • 2016-07-07
      相关资源
      最近更新 更多