我正在使用aSmack库与远程xmpp服务器通信。 我能够发送/接收消息,但我想获取传入消息的时间戳。
请问是否可能实现?因为我无法在这个问题上找到任何信息。
提前致谢。
由于规范要求XMPP消息必须有时间属性:
http://xmpp.org/extensions/xep-0203.html#protocol
请检查<message
的<delay
项:
<delay xmlns='urn:xmpp:delay'
from='capulet.com'
stamp='2002-09-10T23:08:25Z'>
Offline Storage
</delay>
不过获取它看起来有点棘手。一旦重新编译aSmack与一些替换的东西,因此尝试像这里一样获取它:
http://edwin.baculsoft.com/2011/06/how-to-get-offline-messages-timestamp-on-openfire/
DelayInformation inf = null;
try {
inf = (DelayInformation)packet.getExtension("x","jabber:x:delay");
} catch (Exception e) {
log.error(e);
}
// get offline message timestamp
if(inf!=null)
Date date = inf.getStamp();
很可能,您需要检查服务器发送的消息扩展值,并将"jabber:x:delay"
替换为'urn:xmpp:delay'
,就像XMPP规范示例中所示。
但不确定是否有效。
时间戳不是常规消息的一部分,因此您无法提取它。
有一个规范(由@Dmitry提到),但这仅适用于特定类型的消息。通常这些消息不是实时的,比如离线和在创建新订阅时发布节点的最后一个pubsub消息。这些是具有内在延迟的消息。
它也适用于4.1.9版本
DelayInformation inf = null; inf = (DelayInformation)message.getExtension(DelayInformation.ELEMENT,DelayInformation.NAMESPACE); if (inf != null){ Date date = inf.getStamp(); System.out.println("date: "+date); }
DelayInformation delayInformation = forwarded.getDelayInformation();
delayInformation.getStamp().getTime();
对于离线消息: 您将会收到以下消息:
<message xml:lang='en' to='<TO>' from='<FROM>' type='chat' id='3SE7n-39'><delay xmlns='urn:xmpp:delay' from='lakshitnagar.com' stamp='2020-09-05T18:01:01.854690Z'>Offline Storage</delay><offline xmlns='http://jabber.org/protocol/offline'><item node='335'/></offline><body>Hello</body></message>
<message xmlns='jabber:client' to='<TO>' from='<FROM>' id='NzVV5-84' xml:lang='en' type='chat'><archived xmlns='urn:xmpp:mam:tmp' xmlns:stream='http://etherx.jabber.org/streams' by='lakshitnagar@lakshitnagar.com' id='1599328757912003'></archived><body>hello</body></message>