powered by Jive Software

Sending/Recieving Messages

I just got started with Smack and am having a little bit of trouble. I run the server and then wrote two small java programs to send and recieve messages. The reciever creates a chat and then does a chat.nextMessage (). The sender does a chat.sendMessage (“This is a Chat”). The receiver doesn’'t respond when the message is sent to it but in debugger mode the message shows up. Any suggestions. I can send my code if needed.

Message was edited by:

sarora529

From what you’‘ve described, the 2 chat instances will have different thread id’'s - have you set

Chat.isFilteredOnThreadID(false);

in the recieving code? (if not, this would explain the behaviour).

Also, ere you 100% sure that

chat.nextMessage()

is being called before the connection recieves the message?

If you still experience problems, could you post snippets of the code your’'re using?

HTH, Pheet

I tried your suggestions and the messages still don’'t pop up.

This is the Receiver:

public static void main(String[] args) {

ConnectionListener co = null;

XMPPConnection.DEBUG_ENABLED = true;

try {

XMPPConnection conn = new XMPPConnection (“localhost”);

conn.login(“admin”, “motorola”);

Chat chat = new Chat (conn, “sarora529@localhost”);

chat.setFilteredOnThreadID(false);

Message m = chat.nextMessage();

System.out.println (m.getBody());

}

catch (Exception e){

e.printStackTrace();

System.exit(-1);

}

}

This is the Sender:

public static void main(String[] args) {

XMPPConnection.DEBUG_ENABLED = true;

try {

XMPPConnection conn = new XMPPConnection (“localhost”);

conn.login(“sarora529”, “motorola”);

Chat chat = new Chat (conn, “admin@localhost”);

chat.sendMessage(“This is a Test”);

}

catch (Exception e){

e.printStackTrace();

System.exit(-1);

}

}

Try putting the

Chat.setFilteredOnThreadID(false);

at the beginning of the reciever code, like:

try {
Chat.setFilteredOnThreadID(false);
XMPPConnection conn = new XMPPConnection ("localhost"); conn.login("admin", "motorola");

Note that it’'s a capital C for Chat ( setFilteredOnThreadID is a static method so it can be called without creating an instance of the class).

Has sender finished executing before reciever starts?

(Note that you should normally be using PacketListners to listen for incoming messages and chats- see XMPPConnection.addPacketListener(PacketListener packetListener, PacketFilter packetFilter) and the smack documentation.)

I’'m seeing some behaviour that sounds a bit similar…

If the message is sent while the receiver is offline then the receiver never gets the message even though it shows up in the debugger.

In my code all chats use the same thread Id. When a new user logs on & creates a Chat object, I poll for messages (to try to get any that were sent while offline) before calling addMessageListener() to get new messages. But messages sent while offline never arrive…

I’'ve tried creating Chat objects using Connection.createChat and also by using the Chat constructor myself. I also call Chat.setFilteredOnThreadId(false) for good measure but to no avail.

Is there something else I should be doing to get those messages?

The messages are arriving in between the time I create the XMPPConnection and when I create the Chat. Perhaps the XMPPConnection object should maintain a PacketCollector of unclaimed chat messages, then when a Chat is created on that XMPPConnection, the XMPPConnection can pump the messages into the Chat object?

The messages are arriving in between the time I

create the XMPPConnection and when I create the Chat.

That’'s why you should be adding a packetListener to the XMPPConnection before logining in. eg:

XMPPConnection Conn=new XMPPConection("localhost");
Conn.addPacketListener(new MyPacketListener(),
           new MessageTypeFilter(Message.Type.CHAT));
Conn.login("fred", "pass");

Thanks very much Pheet! It works great now.

Matt