文章目录

asmack的基本使用

大家都是程序员,直接上代码了:


//是否连接  connection.isConnected();
//断开连接   connection.disconnect();

登陆
XmppConnection.getConnection().login(accounts, password);
            Presence presence = new Presence(Presence.Type.available);       //通知为登录状态
            XmppConnection.getConnection().sendPacket(presence);
//注册
Registration reg = new Registration();
        reg.setType(IQ.Type.SET);
        reg.setTo(XmppConnection.getConnection().getServiceName());
        reg.setUsername(accounts);
        reg.setPassword(password);
        reg.addAttribute("name", nicheng);
        reg.addAttribute("email", email);

        PacketFilter filter = new AndFilter(new PacketIDFilter(
                                        reg.getPacketID()), new PacketTypeFilter(
                                        IQ.class));
        PacketCollector collector = XmppConnection.getConnection().
        createPacketCollector(filter);
        XmppConnection.getConnection().sendPacket(reg);
        IQ result = (IQ) collector.nextResult(SmackConfiguration
                                        .getPacketReplyTimeout());
                                // Stop queuing results
        collector.cancel();// 停止请求results(是否成功的结果)
或者:
AccountManager amgr = connection.getAccountManager();
            amgr.createAccount(account, "111");

//改变状态,相当于心情、签名等
Presence presence = new Presence(Presence.Type.available);  
        presence.setStatus(status);  
        connection.sendPacket(presence);

//取得分组
Roster roster = conn.getRoster();
Collection<RosterGroup> groups = roster.getGroups();

//直接取得所有好友
Roster roster = XmppConnection.getConnection().getRoster();
roster.getEntries();

//创建聊天
登陆后:
ChatManager cm = connection.getChatManager();       
                //发送消息给pc服务器的好友(获取自己的服务器,和好友)
                Chat newchat = cm.createChat("111@d3studio", null);
                    newchat.sendMessage("hi");

//消息发出拦截
MessageInterceptor mMessageInterceptor = new MessageInterceptor();
XmppConnection.getConnection().addPacketInterceptor(
                mMessageInterceptor,
                new PacketTypeFilter(Message.class));      //第一个为发出消息拦截器,第二个参数为拦截类型

//消息进入拦截
        MessageListener mMessageListener = new MessageListener();
        XmppConnection.getConnection().addPacketListener(mMessageListener,
                new PacketTypeFilter(Message.class));
//文件进入拦截,文件发出不能拦截,拦截后会出现发不出的现象
FileTransferManager fileTransferManager = new FileTransferManager(XmppConnection.getConnection());
        fileTransferManager.addFileTransferListener(new ImgListener());

//发送文件
FileTransferManager manager = new FileTransferManager(XmppConnection.getConnection());  
        OutgoingFileTransfer imgSend = manager.createOutgoingFileTransfer(“jid@server/Smack”);  
        imgSend.sendFile(file,file.getName());

//接收文件,实现监听
FileTransferListener
@Override
        public void fileTransferRequest(final FileTransferRequest request) {
            imgCome = request.accept();
File file = new File(XmppConnection.SAVE_PATH + "/"+ request.getFileName());  //接收文件路径
imgCome.recieveFile(file);
}
文章目录