Search in sources :

Example 1 with ChatManagerListener

use of org.jivesoftware.smack.ChatManagerListener in project Smack by igniterealtime.

the class MultiUserChatTest method testPrivateChat.

public void testPrivateChat() {
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {

            public void chatCreated(Chat chat2, boolean createdLocally) {
                assertEquals("Sender of chat is incorrect", room + "/testbot2", chat2.getParticipant());
                try {
                    chat2.sendMessage("ACK");
                } catch (XMPPException e) {
                    fail(e.getMessage());
                }
            }
        });
        // Start a private chat with another participant            
        Chat chat = muc2.createPrivateChat(room + "/testbot", null);
        StanzaCollector collector = chat.createCollector();
        chat.sendMessage("Hello there");
        Message response = (Message) collector.nextResult(2000);
        assertNotNull("No response", response);
        assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
        assertEquals("Body of response is incorrect", "ACK", response.getBody());
        // User2 leaves the room
        muc2.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) StanzaCollector(org.jivesoftware.smack.StanzaCollector) ChatManagerListener(org.jivesoftware.smack.ChatManagerListener) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

Chat (org.jivesoftware.smack.Chat)1 ChatManagerListener (org.jivesoftware.smack.ChatManagerListener)1 StanzaCollector (org.jivesoftware.smack.StanzaCollector)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Message (org.jivesoftware.smack.packet.Message)1