سؤال

I'm using the Smack API for Java to connect to ejabberd and gtalk servers. I can connect and log in fine, but I want to make 1 minor modification.

How can I change the presence stanza of the logged in user?

For example, given the stanza:

<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>

I'd like to append a new field like this before sending off the packet to someone else:

<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>

Any help would be appreciated.

هل كانت مفيدة؟

المحلول

Call addPacketInterceptor on your XMPPConnection:

connection.addPacketInterceptor(new PacketInterceptor() {
        public void interceptPacket(Packet packet) {
            // modify packet
        }
    }, new PacketTypeFilter(Presence.class));

The docs for addPacketInterceptor say:

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top