【问题标题】:Issue on retrieve the XMPP archived message from ejabberd server(Chat history)从 ejabberd 服务器检索 XMPP 存档消息的问题(聊天历史记录)
【发布时间】:2021-02-21 06:12:24
【问题描述】:

我使用以下方法检索聊天记录。

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)
}

调用此函数后,我收到了两个代表。即如果在 XMPPResultSet 中给出 20 条消息,我收到了 20 次 xmppStreamDidFilterStanza(_ sender: XMPPStream) 方法。

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

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>

但是这个方法永远不会被调用。

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

如果有人遇到此问题并已解决或任何已知要解决的问题,请分享您的答案。

【问题讨论】:

    标签: ios objective-c swift ejabberd xmppframework


    【解决方案1】:

    根据您的代码,我发现您没有在任何地方保存 xmppMAM。试试这个:

    private var xmppMAM: XMPPMessageArchiveManagement?
    
    func getArchieveMessages(forUser user:String){
        xmppMAM = XMPPMessageArchiveManagement.init()
    
        // Your code
    }
    

    【讨论】:

    • 嗨@Dmytryk Skorokhod 感谢您的回答。我更改了 global 中声明的 xmppMAM 之类的代码。但它不起作用。查看我的实例类link
    • 另一个建议:尽量避免使用主队列。我建议您在这样的特殊队列中请求 MAM 消息:private var xmppMessageArchiveManagementQueue: DispatchQueue。此外,我正在继续阅读您的代码以找出潜在的问题并会回来。
    • 请检查问题是否在您的“开始”字段中。尝试检索没有此字段的消息。如果您能够以这种方式检索它们,我们将在开始字段格式(可能是日期格式)中查找问题。第二个字段似乎与我使用的相同。
    • @AbishekThangaraj 早些时候它自去年 4 年以来一直运行良好,但突然停止工作。
    • @RushangPrajapati 请更改 XMPPResultSet 中的最大值。
    【解决方案2】:

    下面的代码之前我用于消息获取委托。完全错了

    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
    }
    

    返回消息是获取历史消息所必需的。以前我在 guard let 部分返回 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
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-16
      • 2017-05-09
      • 2018-06-05
      • 2015-11-05
      • 2015-07-01
      • 2017-01-02
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多