我想检索我发送给XMPP中某人的最后一条消息。
我已经编写了这段代码,但它会提取所有已发送的消息:
let query = try? XMLElement(xmlString: "<query xmlns='urn:xmpp:mam:2'/>")
let iq = XMLElement.element(withName: "iq") as? XMLElement
iq?.addAttribute(withName: "type", stringValue: "set")
iq?.addAttribute(withName: "from", stringValue: "f.talebi@x.ir")
iq?.addAttribute(withName: "max", stringValue: "1")
iq?.addAttribute(withName: "id", stringValue: "GetLastUserMessage")
if let aQuery = query {
iq?.addChild(aQuery)
}
xmppStream.send(iq!)
答案 0 :(得分:1)
首先,您应该具有xmppMessageArchiveManagement模块:
var xmppMAM: XMPPMessageArchiveManagement!
然后激活它
xmppMAM.activate(xmppStream)
之后,您可以使用xmppMessageArchiveManagementDelegate的协议功能
extension someClass: XMPPMessageArchiveManagementDelegate {
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
}
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
}
}
现在您可以创建数据包并发送它们,以上两个功能将捕获服务器的数据包: 警告:您应该使用mam:1而不是mam:2,并对所有检索任务使用messageArchiveManagement。例如:对于检索最后一条消息,请构建此数据包并使用mam进行发送。
`let value = DDXMLElement(name: "value", stringValue: youJid)
let child = DDXMLElement(name: "field")
child.addChild(value)
child.addAttribute(withName: "var", stringValue: "with")
let set = XMPPResultSet(max: 1, before: "")
xmppMam.retrieveMessageArchive(at: nil, withFields: [child], with: set)`