Quantcast
Channel: Ignite Realtime : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 10742

Cannot receive/send message using Smack !

$
0
0

Hey all, I am a new user, I have a fresh and clean install of Linux Ubuntu 12.04 (LTS) and I am trying to create a small program that can contact with Spark IM, using an Openfire local server.

 

To achieve this purpose I have followed these tutorials:

 

1. Installing Openfire with a very basic setup: http://www.javacodegeeks.com/2010/08/openfire-server-installation.html

2. Installing Spark and creating testusers in the Openfiser local server: http://www.javacodegeeks.com/2010/09/openfire-server-configuration.html

3. Creating a Java application that can communicate with the Spark IM client using the Smack API: http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html

 

Everything is fairly simple. The Openfire local server works like a charm, and so does the Spark IM client. However, the Java Code using the Smack API is a complete failure. The only thing that works, is the status and the status message. Sending and receiving messages does not work at all.

 

Following  are the two Java classes I use, the XmppManager and then the XmppTest:

 

XmppManager:

import java.util.Collection; import org.jivesoftware.smack.Chat;import org.jivesoftware.smack.ChatManager;import org.jivesoftware.smack.ConnectionConfiguration;import org.jivesoftware.smack.MessageListener;import org.jivesoftware.smack.Roster;import org.jivesoftware.smack.RosterEntry;import org.jivesoftware.smack.SmackConfiguration;import org.jivesoftware.smack.XMPPConnection;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;import org.jivesoftware.smack.packet.Message;import org.jivesoftware.smack.packet.Presence;import org.jivesoftware.smack.packet.Presence.Type; publicclass XmppManager {     privatestaticfinalint packetReplyTimeout = 500; // millis     private String server;    privateint port;     private ConnectionConfiguration config;    private XMPPConnection connection;     private ChatManager chatManager;    private MessageListener messageListener;     public XmppManager(String server, int port) {        this.server = server;        this.port = port;    }     publicvoid init() throws XMPPException {         System.out.println(String.format("Initializing connection to server " +  server + ", port " + port));         SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);         config = new ConnectionConfiguration(server, port);        config.setSASLAuthenticationEnabled(false);        config.setSecurityMode(SecurityMode.disabled);         connection = new XMPPConnection(config);        connection.connect();         System.out.println("Connected: " + connection.isConnected());         chatManager = connection.getChatManager();        messageListener = new MyMessageListener();     }     publicvoid performLogin(String username, String password) throws XMPPException {        if (connection!=null&& connection.isConnected()) {            connection.login(username, password);        }    }     publicvoid setStatus(boolean available, String status) {         Presence.Type type = available? Type.available: Type.unavailable;        Presence presence = new Presence(type);         presence.setStatus(status);        connection.sendPacket(presence);     }     publicvoid destroy() {        if (connection!=null&& connection.isConnected()) {            connection.disconnect();        }    }     publicvoid sendMessage(String message, String buddyJID) throws XMPPException {        System.out.println(String.format("Sending message " + message + " to user " + buddyJID));        Chat chat = chatManager.createChat(buddyJID, messageListener);        chat.sendMessage(message);    }     publicvoid createEntry(String user, String name) throws Exception {        System.out.println(String.format("Creating entry for buddy " + user + " with name " + name));        Roster roster = connection.getRoster();        roster.createEntry(user, name, null);    }     publicvoid printRoster() throws Exception {        Roster roster = connection.getRoster();        Collection<RosterEntry> entries = roster.getEntries();          for (RosterEntry entry : entries) {            System.out.println(String.format("Buddy:" + entry.getName() + " - Status:" + entry.getStatus()));        }    }      privateclass MyMessageListener implements MessageListener {         @Override        publicvoid processMessage(Chat chat, Message message) {            String from = message.getFrom();            String body = message.getBody();            System.out.println(String.format("Received message " + body + " from " + from));        }     } }

 

XmppTest:

publicclass XmppTest {        publicstaticvoid main(String[] args) throws Exception {                String username = "admin";        String password = "admin";                XmppManager xmppManager = new XmppManager("localhost", 5222);                xmppManager.init();        xmppManager.performLogin(username, password);        xmppManager.setStatus(true, "Hello everyone");                String buddyJID = "user1";        String buddyName = "user1";        xmppManager.createEntry(buddyJID, buddyName);                xmppManager.sendMessage("Hello mate1", "user1@ubuntu-virtualbox.com");        xmppManager.sendMessage("Hello mate2", "user1@ubuntu-virtualbox.com");        xmppManager.sendMessage("Hello mate3", "user1@ubuntu-virtualbox.com");                xmppManager.printRoster();                boolean isRunning = true;                while (isRunning) {            Thread.sleep(50);        }                xmppManager.destroy();            } }

 

 

My Openfire local server has two users:

admin -> with admin privileges

user1 -> a regular guy

 

I am trying to make the "admin" user communicate with "user1", however that seems to be impossible.

Furthermore, when I run the XmppTest class, this is what I get:

 

Initializing connection to server localhost, port 5222
Connected: true
Creating entry for buddy user1 with name user1
Sending message Hello mate1 to user user1@ubuntu-virtualbox.com
Sending message Hello mate2 to user user1@ubuntu-virtualbox.com
Sending message Hello mate3 to user user1@ubuntu-virtualbox.com
Buddy:user1@ubuntu-virtualbox.com - Status:subscribe
Buddy:user1 - Status:subscribe
Buddy:user1 - Status:null

 

Which is weird, because my "admin" account knows about "user1" and has it on the friends list.  Furthermore, on the Spark IM client, "user1" does not receive anything from the admin, even thouh in the code I send 3 messages to "user1".


Also, in the Spark client, when I type something to admin,  nothing is printed in the Eclipse console. I do seem to get a "null" print from time to time due to reasons that seem to be random and beyond me.

 

What am I doing wrong? Is the code sample I use outdated ? I would really appreciate some help :S


Viewing all articles
Browse latest Browse all 10742

Trending Articles