简体   繁体   English

从 ejabberd 服务器检索 XMPP 存档消息的问题(聊天记录)

[英]Issue on retrieve the XMPP archived message from ejabberd server(Chat history)

I used the following method to retrieve the chat history.我使用以下方法来检索聊天记录。

func getArchieveMessages(forUser user:String){
    let xmppMAM = XMPPMessageArchiveManagement.init()
    xmppMAM.addDelegate(self, delegateQueue: .main)
    xmppMAM.activate(stream)
    let xmppDateString = NSDate().addingTimeInterval(-(3 * 24 * 60 * 60)).xmppDateTimeString
    var fields: [XMLElement] = []
    let start = XMPPMessageArchiveManagement.field(withVar: "end", type: nil, andValue: xmppDateString)
    fields.append(start)
     let value = DDXMLElement(name: "value", stringValue: user)
     let child = DDXMLElement(name: "field")
     child.addChild(value)
     child.addAttribute(withName: "var", stringValue: "with")
     let set = XMPPResultSet(max: 20, before: "")
    fields.append(child)
    xmppMAM.retrieveMessageArchive(at: nil, withFields: fields, with: set)
}

After call this function I received the two delegates.调用此函数后,我收到了两个代表。 ie if give 20 messages in XMPPResultSet I received 20 times xmppStreamDidFilterStanza(_ sender: XMPPStream) method.即如果在 XMPPResultSet 中给出 20 条消息,我收到了 20 次xmppStreamDidFilterStanza(_ sender: XMPPStream)方法。

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
    print("didFinishReceivingMessagesWith", resultSet)
}
    func xmppStreamDidFilterStanza(_ sender: XMPPStream) {
    debugPrint("xmppStreamDidFilterStanza")
}

Response in func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) 中的响应

didFinishReceivingMessagesWith <set xmlns="http://jabber.org/protocol/rsm"><count>364</count><first>1603801030936227</first><last>1603948285226175</last></set>

But this method is never called.但是这个方法永远不会被调用。

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
  if let body = message.mamResult?.forwardedMessage{
    print("didReceiveMAMMessage", body)
  }
    print("didReceiveMAMMessage", message)
}

if anyone face this issue and resolved or any known the issue to solve please share your answer.如果有人遇到此问题并已解决或任何已知要解决的问题,请分享您的答案。

Below code previously i used for message get delegate.下面的代码以前我用于消息获取委托。 Its totally wrong它完全错误

func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
 guard let body = (message.body?.replacingOccurrences(of: "\t", with: String.empty))?.replacingOccurrences(of: "\\s+$", with: String.empty, options: .regularExpression) else {
   return nil
 }
 debugPrint(body)
 return message
}

Return the message is necessary for get history messages.返回消息是获取历史消息所必需的。 previously I return nil value in guard let part以前我在guard let part中返回nil值

return nil返回零

func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
 if let forwardedMessage = message.mamResult?.forwardedMessage{
   debugPrint(forwardedMessage)
   return message
 }
 guard let body = (message.body?.replacingOccurrences(of: "\t", with: String.empty))?.replacingOccurrences(of: "\\s+$", with: String.empty, options: .regularExpression) else {
   return nil
 }
 debugPrint(body)
 return message
}

Based on your code I see that you don't save xmppMAM anywhere.根据您的代码,我看到您没有将 xmppMAM 保存在任何地方。 Try this:尝试这个:

private var xmppMAM: XMPPMessageArchiveManagement?

func getArchieveMessages(forUser user:String){
    xmppMAM = XMPPMessageArchiveManagement.init()

    // Your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM