I am trying to setup a simple example for sending and receiving messages with Smack API using Openfire. I have followed this tutorial
Someone asked the same question about the situation here, but the answer mentions the code which I am already using for receiving the chat. Many of these examples using the same method as well.
public class Receiver {
public static void main(String a[]) throws XMPPException, InterruptedException {
XMPPConnection connection = new XMPPConnection("192.168.1.38");
System.out.println(connection);
connection.connect();
connection.login("test2", "123456");
EFLogger.LogInfo("Receiver", "Connected [ " + connection.isConnected() + " ]");
connection.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean b) {
System.out.println("In Message Listener ! ");
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Message [ " + message.toXML());
}
});
try {
chat.sendMessage("Hello");
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
while (true) {
}
}
}
For sending, I am using
public class Main {
public static void main(String[] args) throws Exception {
String username = "test";
String password = "123456";
XmppManager xmppManager = new XmppManager("192.168.1.38", 5222);
xmppManager.init();
xmppManager.performLogin(username, password);
xmppManager.setStatus(true, "Hello everyone");
//As @MrPk suggested I shouldn't use "/Smack"
//String buddyJID = "test2@ef-app2/Smack";
String buddyJID = "test2@ef-app2";
String buddyName = "test";
xmppManager.createEntry(buddyJID, buddyName);
for (int i = 0; i < 10; i++) {
xmppManager.sendMessage("Hello mate from test " + i, "test2");
}
boolean isRunning = true;
while (isRunning) {
Thread.sleep(50);
}
xmppManager.destroy();
}
}
and this class is XMPPManager
/**
This class is responsible for handling all Actions related to Chat Management. Connection, Login, Status, Create Entry and Message Listener.
*/
public class XmppManager {
private static final int packetReplyTimeout = 500; // millis
private String server;
private int 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;
}
public void init() throws XMPPException {
EFLogger.LogInfo("XmppManager", String.format("Initializing connection to server %1$s port %2$d", server, port));
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
// SmackConfiguration.DEBUG = true;
config = new ConnectionConfiguration(server, port);
config.isDebuggerEnabled();
connection = new XMPPConnection(config);
connection.connect();
EFLogger.LogInfo("XmppManager", "Connected: " + connection.isConnected());
chatManager = connection.getChatManager();
//messageListener = new MyMessageListener();
}
public void performLogin(String username, String password) throws XMPPException {
if (connection != null && connection.isConnected()) {
EFLogger.LogInfo("XmppManager", "Before login userName [ " + username + " ] password [ " + password + " ]");
connection.login(username, password);
System.out.printf("Logged in ");
}
}
public void setStatus(boolean available, String status) {
Presence.Type type = available ? Type.available : Type.unavailable;
Presence presence = new Presence(type);
presence.setStatus(status);
connection.sendPacket(presence);
}
public void destroy() {
if (connection != null && connection.isConnected()) {
connection.disconnect();
}
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
EFLogger.LogInfo("XmppManager", String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
Chat chat = chatManager.createChat(buddyJID, messageListener);
chat.sendMessage(message);
}
public void createEntry(String user, String name) throws Exception {
EFLogger.LogInfo("XmppManager", String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
static class MyMessageListener implements MessageListener {
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
EFLogger.LogInfo("XmppManager", String.format("Received message '%1$s' from %2$s", message.getError(), from));
}
}
}
First I thought there must be a problem with buddyID, I have these two users test
and test2
and XMPPdomain is ef-app2
so I set the buddyJID
test2@ef-app2/Smack
smack is default resource.
Edit
@MrPk suggested shouldn't use /Smack , but no luck.
but still, it has no impact I am still unable to receive any message. I am not sure what I am missing. If you are interested in reproducing the same issue, you can find the IntelliJ IDEA project here you can get some more details about the issue here
Help!