Hi,
I am a newbie, developing a basic IM application using the XMPP library.
All the users etc. have been setup using Spark.
For development, I login from my program using “user1” and try to send/receive messages with the user’'s contacts who are on Spark.
From my program I can send messages easily using Chat:
Chat chat = new Chat(connection, “user2@sdd”);
chat.sendMessage(“Hey how are you?”);
System.out.println(chat.getThreadID());
This opens up a new chat window in “user2”’'s Spark client and displays the sent message.
Now, I want to detect incoming messages.
So, based on the documentation and several posts, I wrote the following:
PacketFilter filter = new PacketTypeFilter(Message.class);
PacketListener myListener = new PacketListener()
{
public void processPacket(Packet packet)
{
Message message = (Message)packet;
Message.Type messageType = message.getType();
if (messageType == Message.Type.CHAT)
System.out.println(message.getFrom() + ", " + message.getBody());
else
System.out.println(“Not a chat message”);
}
}};
connection.addPacketListener(myListener, filter);
Now when user2 replies to user1, the incoming message is correctly caught by the listener, and the message gets displayed.
However how do I know who this incoming message belongs to?
There could be several chats runnign simultaneously, and ultimately the received message has to be displayed in a gui window.
Both message.getThread() and chat.getThreadID() return different values.
How do I synchronize sent/received messages?
Another thing:
Where is the following code useful? It never gets executed:
chat.addMessageListener(new PacketListener()
{
public void processPacket(Packet packet)
{
System.out.println(“1 got message in Chat”);
}
});
Please help – been stuck on this.
Thanks,
Karan