smack 小例子

 

import org.jivesoftware.smack.Chat;   
import org.jivesoftware.smack.ChatManager;   
import org.jivesoftware.smack.ConnectionConfiguration;   
import org.jivesoftware.smack.MessageListener;   
import org.jivesoftware.smack.PacketListener;   
import org.jivesoftware.smack.XMPPConnection;   
import org.jivesoftware.smack.XMPPException;   
import org.jivesoftware.smack.filter.PacketFilter;   
import org.jivesoftware.smack.filter.PacketTypeFilter;   
import org.jivesoftware.smack.packet.Message;   
import org.jivesoftware.smack.packet.Packet;   
  
/**  
* Chat prototype using smack as client library. Server side are supported by  
* openfire.  
*   
* @author promise  
* @date 2009-3-30  
*   
*/  
public class FireChat {   
    public static XMPPConnection con;   
    public static Chat newChat;   
    public static ChatManager chatmanager;   
  
    public static void main(String[] args) throws InterruptedException {   
        // XMPPConnection.DEBUG_ENABLED = true;   
        try {   
            // Create a connection to server   
            ConnectionConfiguration config = new ConnectionConfiguration(   
                    "localhost", 5222);   
            con = new XMPPConnection(config);   
  
            // connect and login with the username and pwd on server   
            con.connect();   
            con.login("admin", "123");   
            System.out.println("Authenticated = " + con.isAuthenticated());   
  
            // add a listener to receive all messages   
            addListener();   
  
            chatmanager = con.getChatManager();   
            newChat = chatmanager.createChat("zkp@10.25.101.159/spark",   
                    new MessageListener() {   
                        public void processMessage(Chat chat, Message message) {   
                            System.out.println("I'm sending: "  
                                    + message.getBody());   
                        }   
                    });   
            newChat.sendMessage("hi");   
        } catch (XMPPException e) {   
            e.printStackTrace();   
        } finally {   
        //让线程休眠  然后再关闭连接 
            Thread.sleep(3600000);   
            con.disconnect();   
        }   
    }   
  
    private static void addListener() {   
        // just need Messages   
        PacketFilter filterMessage = new PacketTypeFilter(Message.class);   
  
        PacketListener myListener = new PacketListener() {   
            public void processPacket(Packet packet) {   
                System.out.println("From: " + packet.getFrom() + "\n");   
                System.out.println("Body: " + ((Message) packet).getBody());   
                // when receiving prc's Message, just say something else again   
                // and again, robot   
                try {   
                    newChat.sendMessage("hi again");   
                } catch (XMPPException e) {   
                    e.printStackTrace();   
                }   
            }   
        };   
        // register the listener to the connection   
        con.addPacketListener(myListener, filterMessage);   
    }   
}   


 

 
要提到的是,运行此程序必须先配置好openfire的服务器,然后把用户名等写对!

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值