gpt4 book ai didi

swift - 如何从 XMPPMessage 中提取接收消息体?

转载 作者:行者123 更新时间:2023-11-28 10:08:06 26 4
gpt4 key购买 nike

我收到所有用户发送给我的所有消息。
当我得到结果时,它是 XMPPMessage 类型,我不知道如何从中提取正文

This problem is related to get archived messages.

func getALLMessagesFromServerWithXML() {
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: "id", stringValue: "getAllMesseges")
if let aQuery = query {
iq?.addChild(aQuery)
}

xmppStream.send(iq!)
}

从这个方法得到的结果:

func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
print(message)
}

输出

<message xmlns="jabber:client" to="f.talebi@x/1516292205485357040111042" from="f.talebi@x"><result xmlns="urn:xmpp:mam:2" id="1530957470465122"><forwarded xmlns="urn:xmpp:forward:0"><message xmlns="jabber:client" lang="en" to="a.mardani@xmpp.x.ir" from="f.talebi@x.ir/134788006381643425047394" type="chat"><archived xmlns="urn:xmpp:mam:tmp" by="f.talebi@x.ir" id="1530957470465122"></archived><stanza-id xmlns="urn:xmpp:sid:0" by="f.talebi@x.ir" id="1530957470465122"></stanza-id><body>hi 2018-07-07 09:57:49 +0000</body></message><delay xmlns="urn:xmpp:delay" from="x.ir" stamp="2018-07-07T09:57:50.465122Z"></delay></forwarded></result></message>

如何从此输出中提取正文?对于普通消息,我可以通过 message.body 获取正文,但对于存档消息,我无法使用此代码获取正文。

根据@andesta.erfan 的回答,我添加了这些代码:

变量:

private var archiving = XMPPMessageArchiveManagement()

在初始化()中

archiving = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
archiving?.activate(xmppStream)
archiving?.addDelegate(self, delegateQueue: DispatchQueue.main)

扩展实现:

extension XMPPHelper: XMPPMessageArchiveManagementDelegate {

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
print(message.body())
}

}

但是 xmppMessageArchiveManagement 永远不会被调用,xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) 在这两种情况下都会被调用。当它是存档消息或普通消息时。

最佳答案

对于常规消息,您应该使用:

func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
打印(消息。正文())
}

出于 MAM 的目的,您应该实现 XmppMessageArchiveManagement 及其委托(delegate)。其中一个委托(delegate)方法是这样的:

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage 消息: XMPPMessage) {
打印(消息。正文)
}

你可以用那个打印存档。请注意,您的传出数据包应该是这样的:

`let value = DDXMLElement(name: "value", stringValue: jid)
let child = DDXMLElement(name: "field")
child.addChild(value)
child.addAttribute(withName: "var", stringValue: "with")
let set = XMPPResultSet(max: 1, before: "")
XmppMessageArchiveModule.retrieveMessageArchive(at: nil, withFields: [child], with: set)`

max: 1 告诉 MAM 您只需要特定 jid 的最后一条消息。完成所有这些后,请检查此答案 [ service unavailable error in openfire message archive management

关于swift - 如何从 XMPPMessage 中提取接收消息体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com