I have made a simple client using SMACK api and OpenFire on localhost. As of now, I have two users say X and Y. Whenever anyone logs in to the client, there is a send button which sends a message to user Y. I've checked the XMPP debug manager and I'm able to send/receive messages there. However, I'm unable to display those messages in a JLabel that I have alloted for displaying the messages. If the User Y hits the send button, as per the functionality it sends a message to himself and that comes up on the JLabel. But when user X hits the send button, the message is received, however it is not displayed/updated on the Y's JLabel.
Interestingly, everything works as it should with gtalk configuration.
The structure is as follows:
WelcomeUser.java: Is the welcome form with username and password prompt. On hitting the login button : it sets the ChatBoard class visible and passes on the username password to the constructor of ChatBoard. Code:
chBoard=new ChatBoard(username.getText(),password.getText());
chBoard.setVisible(true);
setVisible(false);
Here is the code for ChatBoard.java:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class ChatBoard extends JFrame implements MessageListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
static String username, password;
static XMPPConnection connection;
private JTextField textField;
static JLabel board = new JLabel("");
static Chat chat;
public void sendChat() {
try {
chat = connection.getChatManager().createChat(
"userY@my-pc/Smack", this);
chat.sendMessage("Hello, This is " + username);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Create the frame.
*/
public ChatBoard(String user, String pass) {
username = user;
password = pass;
////
///JFrame creation code ommited for this thread.
// ////////////////////////////////////////////
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration config = new ConnectionConfiguration(
"127.0.0.1");
connection = new XMPPConnection(config);
try {
connection.connect();
System.out.println("Connected...");
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Connected. Error :" + e.getMessage());
}
try {
connection.login(username, password);
System.out.println("Logged in...");
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Logged in. Error :" + e.getMessage());
}
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for (RosterEntry r : entries) {
System.out.println(r.getUser());
}
// /////////////////////////////////
}
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
board.setText(board.getText() + arg1.getBody());
System.out.println(arg1.getFrom() + ":" + arg1.getBody().toString());
}
}
I'm new to this, so forgive me if there are any silly mistakes. Your help would be really appreciated.
Misc. Details: OpenFire 3.7.1 on localhost with SMACK 3.2.2
Have a look at the bottom of http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html. For incoming chats from another user you have to override ChatManagerListener
Can you set a breakpoint on processMessage and does it hit that breakpoint? If so it might just be a matter of making sure the:
board.setText()
occurs on the Swing event dispatch thread
So try making your processMessage like this :
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
board.setText(board.getText() + arg1.getBody());
}
});
}
catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(arg1.getFrom() + ":" + arg1.getBody().toString());
}