Smack is a library for communicating with XMPP servers to perform instant messaging and chat.
 

package cn.martin.xmpp;

import org.jivesoftware.smack.GroupChat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.AfterClass;

/**
 * 
@author martin
 
*/
public class SmackTest {
    
private static XMPPConnection con;

    @BeforeClass
    
public static void getConnection() throws XMPPException {
        con 
= new XMPPConnection("rabbit");
        con.login(
"martinx""1234");
    }

    @Test
    
public void chatWithSingle() throws XMPPException {
        con.createChat(
"martin@rabbit").sendMessage("Simple Message");
    }

    @Test
    
public void chatWithGroup() throws XMPPException {
        String room 
= "dd@conference.rabbit";
        GroupChat groupChat 
= con.createGroupChat(room);
        groupChat.join(
"martinx");
        Message message 
= groupChat.createMessage();
        message.setBody(
"Group Chat Test");
        groupChat.sendMessage(message);
    }

    @Test
    
public void chatWithReturnedMessage() throws XMPPException {
        Chat chat 
= con.createChat("martin@rabbit");

        Message message 
= chat.createMessage();
        message.setBody(
"Hello Martin");
        message.setProperty(
"favoriteColor""red");
        chat.sendMessage(message);

        
//获取回复
        while (true) {
            Message _message 
= chat.nextMessage();
            chat.sendMessage(_message.getBody());
        }
    }

    @AfterClass
    
public static void closeConnection() {
        con.close();
    }
}

看了http://forum.iteye.com/viewtopic.php?t=19089很是感兴趣,想做一个,明天继续

分享到:
评论