powered by Jive Software

Problem with chat

Hello!

This is my code(my english isn’'t good):

chatmanager.addChatListener(new ChatManagerListener()

{

public void chatCreated(Chat chat, boolean createdLocally)

{

if(!createdLocally)

{

MyRosterEntry e = new MyRosterEntry(chat);

}

}

}

class MyRosterEntry

{

Chat chat;

MyRosterEntry(Chat ch)

{

chat = ch;

chat.addMessageListener(

new MessageListener()

{

public void processMessage(Chat chat, Message message)

{

newMessage(message); //

}

});

}

}

If someone write to me who doesn’‘t exist in my roster, chatManagerListener call ChatCreated then is created new entry in my roster but message listener don’'t

intercept the new message only first message. Second client is Psi. Psi don’'t use threadID,how to avoid this?.

jacek- I have had similar problems with the Chat implementation and threadId. I am moving away from Chat and ChatManager and towards processing Message packets directly. You might want to consider that route as well.

It wasn’‘t totally clear to me what you were trying to do by adding the RosterEntry when you receive a new Chat- unless you go through the invite process, that entry probably won’‘t do you much good since it does not reflect the Presence status of the buddy. I think you don’'t need the entry to simply send/receive Message packets, but I could be wrong.

Probably not the answer you were looking for, but I hope it helps.

Cheers,

Dan

Well, in your example, your messageListener just creates another Message object. If that’‘s all, you won’'t “see” any activity. Try making a JOptionPane that display the body of the Message that is sent to you in the listener

message.getBody();

Also, a message you send does not get caught by a MessageListener, you have to make sure you show it in your GUI yourself.

I’‘m using the ChatManager and MessageListener, and it’‘s working fine so far. I only couldn’‘t get ChatState to work, but that’'s no big deal.

I didn’‘t paste the whole code. I have GUI. You understood me bad. My ChatListener create a new position in my roster and add messageListener. messageListener didn’‘t catch a message but ChatListener catch a new chat. This problem is only for users which doesn’'t exist in my roster. When I create roster view from roster, messageListener is automatically added and work fine.

Can you paste some code which show me how correctly use ChatListener and messageListener?

I see. Do you keep all the chat session objects referenced? Here’'s how I did it for our application. The ChatSession class contains the Chat object and the chat GUI associated.

Then I keep all ChatSessions in a Set. Any time a new chat is created or a message arrives, I look in the Set if a session with that participant already exists (no threadID).

private class ChatSession {
        Chat thisChat;
        Op3SupportChatGUI thisGUI;
        // etc.
    }     private static Set<ChatSession> allChats;     public void chatCreated(Chat chat, boolean createdLocal) {
        // keep track of all sessions
        final ChatSession chatsess = findSessionParticipant(chat.getParticipant());         if (chatsess != null) {
            chatsess.thisChat.removeMessageListener(this);
                        chatsess.thisChat = chat;
            if (chatsess.thisChat.getListeners().isEmpty() ||
                    !chatsess.thisChat.getListeners().contains(this))
                chatsess.thisChat.addMessageListener(this);
            chatsess.showChatGUI();
        } else {
            // create new session
            final ChatSession sess = new ChatSession(chat, appSettings.keepChatLog());
            if (sess.thisChat.getListeners().isEmpty() ||                     !sess.thisChat.getListeners().contains(this))
                sess.thisChat.addMessageListener(this);
            allChats.add(sess);
            sess.showChatGUI();
        }
    }     public void processMessage(Chat chat, Message message) {
        // deliver message to corresponding gui
        // find chat session in set
        final ChatSession chatsess = findSessionParticipant(chat.getParticipant());         if (chatsess != null)
            chatsess.messageDo(message);
        else {
            // create new session
            ChatSession sess = new ChatSession(chat, appSettings.keepChatLog());
            if (sess.thisChat.getListeners().isEmpty() ||
                    !sess.thisChat.getListeners().contains(this))
                sess.thisChat.addMessageListener(this);
            allChats.add(sess);
            sess.messageDo(message);
        }
    }     /** Returns chat session object with participant, null if not found.
     * @param jid other participant */
    private ChatSession findSessionParticipant(String jid) {
        jid = StringUtils.parseBareAddress(jid);
        final Iterator<ChatSession> iter = allChats.iterator();
        while (iter.hasNext()) {
            ChatSession chatsess = iter.next();
            if (jid.equalsIgnoreCase(StringUtils.parseBareAddress(
                    chatsess.thisChat.getParticipant()))) {
                return chatsess;
            }
        }
        return null;
    }

This works fine so far. Note that you shouldn’'t add multiple listeners, otherwise you will see duplicates.