• XMPP——Smack[4]状态,心情,头像更改 (转) CVT


    转自:http://blog.csdn.net/wklken/article/details/6460117

    这里写完,最基本的IM功能也就完了,

    还剩下个发送接收文件,离线消息扩展等等

    呵呵,三天时间,看的不是很深入,欢迎大家补充呀

    1. 修改自身状态

    包括上线,隐身,对某人隐身,对某人上线

    1. public static void updateStateToAvailable(XMPPConnection connection)  
    2.     {  
    3.         Presence presence = new Presence(Presence.Type.available);  
    4.         connection.sendPacket(presence);  
    5.      }  
    6.       
    7.     public static void updateStateToUnAvailable(XMPPConnection connection)  
    8.     {  
    9.         Presence presence = new Presence(Presence.Type.unavailable);  
    10.         connection.sendPacket(presence);  
    11.         }  
    12.       
    13.     public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)  
    14.     {  
    15.         Presence presence = new Presence(Presence.Type.unavailable);  
    16.         presence.setTo(userName);  
    17.         connection.sendPacket(presence);  
    18.     }  
    19.     public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)  
    20.     {  
    21.         Presence presence = new Presence(Presence.Type.available);  
    22.         presence.setTo(userName);  
    23.         connection.sendPacket(presence);  
    24.   
    25.     }  
    1. 心情修改

      

    1. /** 
    2.      * 修改心情 
    3.      * @param connection 
    4.      * @param status 
    5.      */  
    6.     public static void changeStateMessage(XMPPConnection connection,String status)  
    7.     {  
    8.         Presence presence = new Presence(Presence.Type.available);  
    9.         presence.setStatus(status);  
    10.         connection.sendPacket(presence);  
    11.       
    12.     }  
    1. 修改用户头像

    有点麻烦,主要是读入图片文件,编码,传输之

    1. public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException  
    2.     {  
    3.       
    4.         VCard vcard = new VCard();  
    5.         vcard.load(connection);  
    6.           
    7.             byte[] bytes;  
    8.             
    9.                 bytes = getFileBytes(f);  
    10.                 String encodedImage = StringUtils.encodeBase64(bytes);  
    11.                 vcard.setAvatar(bytes, encodedImage);  
    12.                 vcard.setEncodedImage(encodedImage);  
    13.                 vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"  
    14.                         + encodedImage + "</BINVAL>", true);  
    15.                   
    16.                   
    17.                 ByteArrayInputStream bais = new ByteArrayInputStream(  
    18.                         vcard.getAvatar());  
    19.                 Image image = ImageIO.read(bais);  
    20.                 ImageIcon ic = new ImageIcon(image);  
    21.                    
    22.              
    23.             
    24.             vcard.save(connection);  
    25.              
    26.     }  
    27.       
    28.     private static byte[] getFileBytes(File file) throws IOException {  
    29.         BufferedInputStream bis = null;  
    30.         try {  
    31.             bis = new BufferedInputStream(new FileInputStream(file));  
    32.             int bytes = (int) file.length();  
    33.             byte[] buffer = new byte[bytes];  
    34.             int readBytes = bis.read(buffer);  
    35.             if (readBytes != buffer.length) {  
    36.                 throw new IOException("Entire file not read");  
    37.             }  
    38.             return buffer;  
    39.         } finally {  
    40.             if (bis != null) {  
    41.                 bis.close();  
    42.             }  
    43.         }  
    44. }  
    1. 补充,用户状态的监听

    即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器

    1. final Roster roster = Client.getRoster();  
    2.           
    3.         roster.addRosterListener(  
    4.                 new RosterListener() {  
    5.   
    6.                     @Override  
    7.                     public void entriesAdded(Collection<String> arg0) {  
    8.                         // TODO Auto-generated method stub  
    9.                         System.out.println("--------EE:"+"entriesAdded");  
    10.                     }  
    11.   
    12.                     @Override  
    13.                     public void entriesDeleted(Collection<String> arg0) {  
    14.                         // TODO Auto-generated method stub  
    15.                         System.out.println("--------EE:"+"entriesDeleted");  
    16.                     }  
    17.   
    18.                     @Override  
    19.                     public void entriesUpdated(Collection<String> arg0) {  
    20.                         // TODO Auto-generated method stub  
    21.                         System.out.println("--------EE:"+"entriesUpdated");  
    22.                     }  
    23.   
    24.                     @Override  
    25.                     public void presenceChanged(Presence arg0) {  
    26.                         // TODO Auto-generated method stub  
    27.                         System.out.println("--------EE:"+"presenceChanged");  
    28.                     }     
    29.                       
    30.                 });  
    31.               

    下一篇主要是文件传输和接收

  • 相关阅读:
    【BZOJ 4151 The Cave】
    【POJ 3080 Blue Jeans】
    【ZBH选讲·树变环】
    【ZBH选讲·拍照】
    【ZBH选讲·模数和】
    【CF Edu 28 C. Four Segments】
    【CF Edu 28 A. Curriculum Vitae】
    【CF Edu 28 B. Math Show】
    【CF Round 439 E. The Untended Antiquity】
    【CF Round 439 C. The Intriguing Obsession】
  • 原文地址:https://www.cnblogs.com/openfire/p/3029277.html
Copyright © 2020-2023  润新知