我正在使用 Smack API for Java 连接到 ejabberd 和 gtalk 服务器。我可以正常连接和登录,但我想做 1 处小修改。
如何更改已登录用户的状态节?
例如,给定节:
<presence to="someone@someServer.com/androidd5a3arer3"
from="me@someServer.com/Smack">
<status></status>
<priority>24</priority>
<show>away</show>
<c xmlns="http://jabber.org/protocol/caps"></c>
<x xmlns="vcard-temp:x:update">
<photo>asefe3a33e</photo>
</x>
</presence>
我想在将数据包发送给其他人之前附加一个这样的新字段:
<presence to="someone@someServer.com/androidd5a3arer3"
from="me@someServer.com/Smack">
<status></status>
<priority>24</priority>
<show>away</show>
<c xmlns="http://jabber.org/protocol/caps"></c>
<x xmlns="vcard-temp:x:update">
<photo>asefe3a33e</photo>
</x>
<NEW_FIELD> NEW STUFF HERE </NEW_FIELD>
</presence>
如有任何帮助,我们将不胜感激。
最佳答案
在您的 XMPPConnection 上调用 addPacketInterceptor:
connection.addPacketInterceptor(new PacketInterceptor() {
public void interceptPacket(Packet packet) {
// modify packet
}
}, new PacketTypeFilter(Presence.class));
docs对于 addPacketInterceptor 说:
Registers a packet interceptor with this connection. The interceptor will be invoked every time a packet is about to be sent by this connection. Interceptors may modify the packet to be sent. A packet filter determines which packets will be delivered to the interceptor.
关于java - 在 XMPP 客户端中更改传出状态节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6335994/