How to get the Chat in a PacketListener to return a response?

I’‘ve got a standard request/reponse scenario, in which a PacketListener has received a packet of type Message.class. I need to return a sponse in the same chat, but haven’'t found any examples of doing so. Is there a method to get to the Chat object for a packet, so I can createMessage() and sendMessage()?

Or am I missing something?



I did get this working, and it is a lot simpler than I thought. No Map of Chats or anything like that. The trick is not to think about the Chat object as a connection as I was. It is a sequence of messages. So my packet listener gets a Message (filtered, so I know it is a message), and I do the following:

Message msg = (Message)packet;

Chat chat = new Chat(connection, msg.getFrom(), msg.getThread());

Message reply = chat.createMessage();


That did it. The constructor with the thread ID keeps this message in the same “Chat” thread. My understanding is that if not specified it will use the last used thread, which might work as well based upon your situation. This is working for me.



I understand how you made this work.

But what happens when you are initiating the chat?

The first message gets sent using:

chat.sendMessage(“Hey hw’'s it going?”);

In this case when your buddy replies, how do you know which message he is replying to?

The thread ID in the message, will be different from the one used by the chat object.

Eg. you could be a in a group chat with the same buddy, and in a private chat.

Any ideas??

Check out this post: