从 ejabberd 服务器(聊天记录)检索 XMPP 存档消息的问题
Issue on retrieve the XMPP archived message from ejabberd server(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)
}
调用此函数后,我收到了两个代表。即如果在 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)
}
如果有人遇到此问题并已解决或任何已知问题需要解决,请分享您的答案。
根据您的代码,我发现您没有在任何地方保存 xmppMAM。试试这个:
private var xmppMAM: XMPPMessageArchiveManagement?
func getArchieveMessages(forUser user:String){
xmppMAM = XMPPMessageArchiveManagement.init()
// Your code
}
下面的代码是我之前用于消息获取委托的。完全错误
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 该消息是获取历史消息所必需的。以前我 return 在 guard let part
中没有值
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
}
我是用下面的方法找回聊天记录的
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)
}
如果有人遇到此问题并已解决或任何已知问题需要解决,请分享您的答案。
根据您的代码,我发现您没有在任何地方保存 xmppMAM。试试这个:
private var xmppMAM: XMPPMessageArchiveManagement?
func getArchieveMessages(forUser user:String){
xmppMAM = XMPPMessageArchiveManagement.init()
// Your code
}
下面的代码是我之前用于消息获取委托的。完全错误
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 该消息是获取历史消息所必需的。以前我 return 在 guard let part
中没有值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
}