Search code examples
javaandroiddebuggingxmppsmack

XMPP Unable to set up a Listener


I've successfully connected to XMPP server (from android XMPP client), and I can send messages to a user, but I can't receive responses from that same user.

I'm sending messages like this:

  public void send_message(String message, String buddy) throws XMPPException {
    buddy += "@localhost";

    /* send message to user */
    Log.w("Sending mesage " + message + " to user " + buddy, "0");
    chat = chatManager.createChat(buddy, messageListener);
    chat.sendMessage(message);
  }

I'm passing the messageListener to the createChat function. The MessageListener's class is:

class XMPPMessageListener implements MessageListener {
  private String from;
  private String body;

  public void processMessage(Chat chat, Message message) {
    this.from = message.getFrom();
    this.body = message.getBody();
    Log.w("*****Received message" + body + " from " + from, "0*****");
  }

}

When sending a message to a user I'm getting the following debugging output:

W/Sending mesage play to user test@localhost(  823): 0
D/SMACK   (  823): 10:43:54 AM SENT (1156346368): <message id="vwaJX-15" to="test@localhost" from="eleano@localhost/Smack" type="chat"><body>test</body><thread>249ke0</thread></message>
D/SMACK   (  823): 10:43:54 AM RCV  (1156346368): <presence id="vwaJX-12" to="eleano@localhost/Smack" from="eleano" type="error"><error code="404" type="cancel"><remote-server-no
D/SMACK   (  823): 10:43:54 AM RCV  (1156346368): t-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>
D/SMACK   (  823): 10:43:54 AM RCV  (1156346368): <presence id="vwaJX-14" to="eleano@localhost/Smack" from="test" type="error"><error code="404" type="cancel"><remote-server-not-
D/SMACK   (  823): 10:43:54 AM RCV  (1156346368): found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>

And the message "test" is displayed to the connected user test (in this case). I'm sending the message from a user eleano. We can also see there's a debug output "ending mesage play to user test@localhost" being displayed on the screen, indicating that my function is indeed called successfully.

Upon receiving the message from test to eleano, I only get this debug output:

D/SMACK   (  823): 10:44:00 AM RCV  (1156346368): <message id="58Fjj-64" to="eleano@localhost/Smack" from="test@localhost/Spark 2.6.3" type="chat"><body>yes</body><thread>0tlK7o<
D/SMACK   (  823): 10:44:00 AM RCV  (1156346368): /thread><x xmlns="jabber:x:event"><offline/><composing/></x></message>

But the user eleano is not receiving the message. We can also notice there's no:

Log.w("*****Received message" + body + " from " + from, "0*****");

being displayed on the screen, so the MessageListener is never called. Why is that? I've set it up correctly as it says in documentation.

Any ideas are welcome. And thanks.


Solution

  • Thanks for pointing that out. Your observation led me to setup a listener on connection (and to listen for packages of type Char), rather than setting a listener on the chat object itself.

    Now my code looks like the following. I'm sending the packages like this:

    Message m = new Message(buddy, Message.Type.chat);
    m.setBody(message);
    connection.sendPacket(m);
    

    And receiving the messages like the following:

      /* packet listener: listen for incoming messages of type CHAT on the connection (whatever the buddy) */
      PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
      xmppManager.connection.addPacketListener(new PacketListener() { 
        public void processPacket(Packet packet) {
          Message mes = (Message)packet;
          Log.w("***"+mes.toString()+"***","0");
        }
      }, filter);
    

    And it works. I can send messages to the user and receive them just fine.