1
votes

I am developing a Java App to chat with devices in XMPP.

Given the logs I am able to send messages but I can't receive them

Here is my code

XMPPListener.java

package messaging;

import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class XMPPListener implements MessageListener {

    AbstractXMPPConnection connection;

    public XMPPListener(String host, int port, String serviceName){

        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder().
                setHost(host)
                .setServiceName(serviceName)
                .setSecurityMode(SecurityMode.ifpossible)
                .setPort(port)
                .setDebuggerEnabled(true)
                .build();
        this.connection = new XMPPTCPConnection(config);

    }

    public void connect() {
        try {
            this.connection.connect();
        } catch (SmackException | IOException | XMPPException e) {
            e.printStackTrace();
        }
    }

    public void login(String userName, String password) throws XMPPException {
        try {
            this.connection.login(userName, password);
        } catch (SmackException | IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String message, String to) throws XMPPException {
          Chat chat = ChatManager.getInstanceFor(connection).createChat(to);
          try {
              chat.sendMessage(message);
          } catch (NotConnectedException e) {
              e.printStackTrace();
          }

    }

    public void listenChat(String name){
        ChatManager manager = ChatManager.getInstanceFor(this.connection);
        manager.addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(Chat chat, boolean createdLocally) {
                System.out.println("Created chat");
                chat.addMessageListener(new ChatMessageListener() {

                    @Override
                    public void processMessage(Chat chat, Message message) {
                        System.out.println(message.getBody());

                    }
                });

            }
        });
    }

    public void disconnect() {
        connection.disconnect();
    }

    @Override
    public void processMessage(Message message) {
        System.out.println("Received something: " + message.getBody());


    }

}

Main.java

XMPPListener xmppListener = new XMPPListener("ajabber.me",5222,"ajabber.me");
        try {
            xmppListener.connect();
            xmppListener.login(user, password);
            xmppListener.listenChat(to);
            while(true){
            xmppListener.sendMessage("Spack me spack me, Ho !", to);
            Thread.sleep(10000);
            }
        } catch (XMPPException e2) {
            e2.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In this code I connect to a random server i found here : list of servers. Then I try to send some messages I have the folowing logs

11:51:56 AM SENT (0): <stream:stream xmlns='jabber:client' to='ajabber.me' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
11:51:56 AM RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='11102383658626487319' from='ajabber.me' version='1.0' xml:lang='en'><stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-
one.net/en/ejabberd/' ver='9BNWaDsRr/HNe8AdlF+JvcDY2L0='/><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls></stream:features>
11:51:56 AM SENT (0): <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>
11:51:56 AM RECV (0): <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
11:51:57 AM SENT (0): <stream:stream xmlns='jabber:client' to='ajabber.me' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
11:51:57 AM RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='9208753033211689749' from='ajabber.me' version='1.0' xml:lang='en'>
11:51:57 AM RECV (0): <stream:features><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='9BNWaDsRr/HNe8AdlF+JvcDY2L0='/><register xmlns='http://jabber.org/features/iq-register'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism><mechanism>DIGEST-MD5</mechanism><mechanism>X-OAUTH2</mechanism><mechanism>SCRAM-SHA-1</mechanism></mechanisms></stream:features>
chat created
11:51:57 AM SENT (0): <message to='[email protected]' id='9ptjB-3' type='chat'><body>coucou</body><thread>d0b9f5a3-515a-4d53-9795-ca112962d4f8</thread></message>
chat created
11:51:59 AM SENT (0): <message to='[email protected]' id='9ptjB-4' type='chat'><body>coucou</body><thread>a6dbc7a0-2b3a-4a30-9be6-2b1e2995cadb</thread></message>

Given those logs, I guess the messages are sent and the chat is also created but it seems that I never receive the messages. Could you guys help me solve this problem ?

I run java 1.8 and Smack 4.7

EDIT

I have tried using Spark : I manage to talk with my application (in a Spark client), I also have edited my code as a new version

Here is the proof with a wonderful screenshot

SparkClient

However I still cannot retrieve my messages : I see them in the logs, but the method in the chat listener is not called

LOGS

03:17:22 PM RECV (0): <message from='[email protected]/Spark' to='[email protected]/Smack' id='NdNHr-150' type='chat'><body>Wololo</body><thread>5292f083-bba9-4a63-ab40-9718b5e00bd0</thread><x xmlns='jabber:x:event'><offline/><composing/></x></message>

LAST EDIT

My code is working with the one above, a special thanks to MrKp

1

1 Answers

1
votes

Assumptions:

  • Server exist (why not install a localmachine openfire or whatelse?)
  • User "sender" is correctly logged in on this server
  • User "receiver" is correctly registered in the server
  • You control with 2 clients user "sender" and user "receiver" (why not install as second client Spark?) or, in alternative, "reciver" it's a working bot in echo mode.

You'll cannot receive your outgoing messages in "chat", but you'll do in groupchats. In chats you'll be able to set a listener for "reciver" messages.

About your code and how to fix:

You are trying to make a connect and register a listener of a chat before login and without login.

Correct order:

  1. connect
  2. login
  3. new Chat
  4. ChatManager.getInstance(connection).addChatListener

now you'll be able to listen incoming messages on this chat, when "reciver" will send you one