XMPPFramework - 检索归档邮件从服务器的Openfire(XMPPFramework

2019-06-14 18:24发布

我使用XMPP和Openfire的服务器开发的iPhone聊天应用的Openfire服务器存储用户之间的所有聊天记录,但是当我尝试找回聊天记录为特定的用户我只得到了日期和聊天消息的数量,但不实际的短信

我已经安装了开放档案插件上的Openfire邮件存档

这是我所传递给的Openfire服务器的节

    <iq type='get' id='pk1'>
    <list xmlns='urn:xmpp:archive'
    with='piyush@openfire'>
    <set xmlns='http://jabber.org/protocol/rsm'>
    <max>30</max>
    </set>
    </list>
    </iq>

这是我从服务器接收到的结果

  <iq type="result" id="pk1" to="vivek@openfire/iphone">
  <list xmlns="urn:xmpp:archive">
  <chat with="piyush@openfire" start="2012-07-04T13:16:12.291Z"/>
  <chat with="piyush@openfire" start="2012-07-05T08:25:31.555Z"/>
  <chat with="piyush@openfire" start="2012-07-05T12:38:24.098Z"/>
  <set xmlns="http://jabber.org/protocol/rsm">
  <first index="0">15</first>
  <last>25</last>
  <count>3</count>
  </set>
  </list>
  </iq>

这就是我想和我预期的结果

 <iq type='result' to='vivek@openfire/iphone' id='page1'>
 <chat xmlns='urn:xmpp:archive'
    with='piyush@openfire'
    start='2012-07-04T13:16:12.291Z'
    subject='She speaks!'
    version='4'>
<from secs='0'><body>Art thou not Romeo, and a Montague?</body></from>
<to secs='11'><body>Neither, fair saint, if either thee dislike.</body></to>
.
[98 more messages]
.
<from secs='9'><body>How cam'st thou hither, tell me, and wherefore?</body></from>
<set xmlns='http://jabber.org/protocol/rsm'>
  <first index='0'>0</first>
  <last>99</last>
  <count>217</count>
</set>

请帮我出得到期望的结果

谢谢

Answer 1:

你必须做一个请求<retrieve> (见http://xmpp.org/extensions/xep-0136.html ),那么你可以从接收到的特定时间<list>的结果。 例如:

发送:

    <iq type='get' id='pk1'>
       <list xmlns='urn:xmpp:archive'
               with='piyush@openfire'>
        <set xmlns='http://jabber.org/protocol/rsm'>
            <max>30</max>
        </set>
      </list>
   </iq>

接收:

 <iq type="result" id="pk1" to="vivek@openfire/iphone">
     <list xmlns="urn:xmpp:archive">
      <chat with="piyush@openfire" start="2012-07-04T13:16:12.291Z"/>
      <chat with="piyush@openfire" start="2012-07-05T08:25:31.555Z"/>
      <chat with="piyush@openfire" start="2012-07-05T12:38:24.098Z"/>
      <set xmlns="http://jabber.org/protocol/rsm">
          <first index="0">15</first>
           <last>25</last>
           <count>3</count>
      </set>
    </list>
 </iq>            

现在你选择的一个start S和发送(日期和时间必须是需要付出):

  <iq type='get' id='pk1'>
    <retrieve xmlns='urn:xmpp:archive'
        with='piyush@openfire''
        start='2012-07-04T13:16:12.291Z'>
     <set xmlns='http://jabber.org/protocol/rsm'>
       <max>100</max>
     </set>
    </retrieve>
 </iq>

您将收到这样的事情(取决于最大值 - >最大= 30,体= 30):

   <iq type='result' to='vivek@openfire/iphone' id='page1'>
      <chat xmlns='urn:xmpp:archive'
             with='piyush@openfire'
              start='2012-07-04T13:16:12.291Z'
            subject='She speaks!'
       version='4'>
         <from secs='0'><body>Art thou not Romeo, and a Montague?</body></from>
         <to secs='11'><body>Neither, fair saint, if either thee dislike.</body></to>
          .
          [28 more messages]
          .
         <from secs='9'><body>How cam'st thou hither, tell me, and therefore?           </body>   
         </from>
      <set xmlns='http://jabber.org/protocol/rsm'>
       <first index='0'>0</first>
       <last>29</last>
       <count></count>
     </set>
  <iq>


Answer 2:

要检索聊天的具体时间

发送到获取时间:

 NSXMLElement *iq1 = [NSXMLElement elementWithName:@"iq"];
 [iq1 addAttributeWithName:@"type" stringValue:@"get"];
 [iq1 addAttributeWithName:@"id" stringValue:@"pk1"];
 NSXMLElement *retrieve = [NSXMLElement elementWithName:@"list" xmlns:@"urn:xmpp:archive"];
 [retrieve addAttributeWithName:@"with" stringValue:@"amit@openfire"];
 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];


 [xmppStream sendElement:iq1];

要检索的聊天记录

使用开始起并发送:

 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:@"amit@openfire"];
 [retrieve addAttributeWithName:@"start" stringValue:@"2013-11-18T05:11:53.460Z"];
 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];
 [xmppStream sendElement:iq1];


Answer 3:

首先,从Openfire的,你需要安装打开存档插件,因为插件监控中仅仅是监控中和记录聊天记录在管理面板找回聊天记录所以一旦你安装打开存档,您将无法与代码“500”得到任何错误。

您可以下载并学习从下面的链接安装打开存档。

https://nexus.reucon.com/content/repositories/opensource-snapshots/com/reucon/openfire/plugins/archive/

https://maven.reucon.com/projects/public/archive/

在上面的代码中的另一个问题是,当你在请求中提到的开始标记则具有这就是为什么它返回错误代码“404”的确切时间戳记聊天匹配。 我开始中省略从我的要求,标签,写下列返回整个聊天记录与用户代码。

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]; 

在这里,这将在用户拉胡尔和当前登录的用户之间的XML响应返回整个聊天记录。

欲了解更多详细信息,请参阅本博客http://question.ikende.com/question/363439343236313430



Answer 4:

斯威夫特版本来去的Openfire服务器请求存档邮件:

func getArchieveMessages(){
        let iQ = DDXMLElement.elementWithName("iq")
        iQ.addAttributeWithName("type", stringValue: "get")
        iQ.addAttributeWithName("id", stringValue: "page1")
        let list = DDXMLElement.elementWithName("retrieve")
        list.addAttributeWithName("xmlns", stringValue: "urn:xmpp:archive")
        list.addAttributeWithName("with", stringValue: "partner@domain")
        let set = DDXMLElement.elementWithName("set")
        set.addAttributeWithName("xmlns", stringValue: "http://jabber.org/protocol/rsm")
        let max = DDXMLElement.elementWithName("max")
        max.addAttributeWithName("xmlns", stringValue: "http://jabber.org/protocol/rsm")
        //(max as! DDXMLElement).setStringValue("30")
        (set as! DDXMLElement).addChild(max as! DDXMLNode)
        list.addChild(set as! DDXMLNode)
        iQ.addChild(list as! DDXMLNode)
        xmppStream.sendElement(iQ as! DDXMLElement)
    }

响应:

func xmppStream(sender: XMPPStream!, didReceiveIQ iq: XMPPIQ!) -> Bool {
        let chat = iq.elementForName("chat")
        let chats = (chat as DDXMLElement).children()
        for chat in chats{
            let msg = chat
            let body = (msg as! DDXMLElement).elementForName("body")
            if body != nil{
                if body.stringValue() != nil{
                    //print(body.stringValue()!)
                    chatMessages.append(body.stringValue()!)
                    if msg.attributeForName("jid") == nil{
                        type.append("Send")
                    }
                    else{
                        type.append("Receive")
                    }
                }
            }
        }
        print("Did receive IQ")
        return false
    }

*用是要获取其存档邮件的人的JID



Answer 5:

这个例子中的斯威夫特4存档的邮件

声明并初始化变量XMPPMessageArchivingCoreDataStorage,我初始化XMPPStream

var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
var xmppMessageArchiving: XMPPMessageArchiving?

xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
    xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)

    xmppMessageArchiving?.clientSideMessageArchivingOnly = true
    xmppMessageArchiving?.activate(stream)
    xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)

这样,每当有消息到达时,这会导致它被而不需要做任何事情都要存档。

然后,检索归档的消息

func RecibedMessageArchiving(idFriend: String) {

        let JabberIDFriend = idFriend   //id friend chat, example test1@example.com


        let moc = xmppMessageStorage?.mainThreadManagedObjectContext
        let entityDescription = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: moc!)
        let request = NSFetchRequest<NSFetchRequestResult>()
        let predicateFormat = "bareJidStr like %@ "
        let predicate = NSPredicate(format: predicateFormat, JabberIDFriend)

        request.predicate = predicate
        request.entity = entityDescription

        //jabberID id del usuario, cliente
        var jabberIDCliente = ""
        if let jabberj = globalChat.value(forKey: "jabberID"){
            jabberIDCliente = jabberj as! String
        }


        do {
            let results = try moc?.fetch(request)

            for message: XMPPMessageArchiving_Message_CoreDataObject? in results as? [XMPPMessageArchiving_Message_CoreDataObject?] ?? [] {

                var element: DDXMLElement!
                do {
                    element = try DDXMLElement(xmlString: (message as AnyObject).messageStr)
                } catch _ {
                    element = nil
                }

                let body: String
                let sender: String
                let date: NSDate
                let isIncomings: Bool


文章来源: XMPPFramework - Retrieve Archived Messages From Openfire Server