這里寫(xiě)完,最基本的IM功能也就完了,
還剩下個(gè)發(fā)送接收文件,離線(xiàn)消息擴展等等
呵呵,三天時(shí)間,看的不是很深入,歡迎大家補充呀
1. 修改自身狀態(tài)
包括上線(xiàn),隱身,對某人隱身,對某人上線(xiàn)
- public static void updateStateToAvailable(XMPPConnection connection)
- {
- Presence presence = new Presence(Presence.Type.available);
- connection.sendPacket(presence);
- }
- public static void updateStateToUnAvailable(XMPPConnection connection)
- {
- Presence presence = new Presence(Presence.Type.unavailable);
- connection.sendPacket(presence);
- }
- public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
- {
- Presence presence = new Presence(Presence.Type.unavailable);
- presence.setTo(userName);
- connection.sendPacket(presence);
- }
- public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
- {
- Presence presence = new Presence(Presence.Type.available);
- presence.setTo(userName);
- connection.sendPacket(presence);
- }
2. 心情修改
- /**
- * 修改心情
- * @param connection
- * @param status
- */
- public static void changeStateMessage(XMPPConnection connection,String status)
- {
- Presence presence = new Presence(Presence.Type.available);
- presence.setStatus(status);
- connection.sendPacket(presence);
- }
3. 修改用戶(hù)頭像
有點(diǎn)麻煩,主要是讀入圖片文件,編碼,傳輸之
- public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException
- {
- VCard vcard = new VCard();
- vcard.load(connection);
- byte[] bytes;
- bytes = getFileBytes(f);
- String encodedImage = StringUtils.encodeBase64(bytes);
- vcard.setAvatar(bytes, encodedImage);
- vcard.setEncodedImage(encodedImage);
- vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
- + encodedImage + "</BINVAL>", true);
- ByteArrayInputStream bais = new ByteArrayInputStream(
- vcard.getAvatar());
- Image image = ImageIO.read(bais);
- ImageIcon ic = new ImageIcon(image);
- vcard.save(connection);
- }
- private static byte[] getFileBytes(File file) throws IOException {
- BufferedInputStream bis = null;
- try {
- bis = new BufferedInputStream(new FileInputStream(file));
- int bytes = (int) file.length();
- byte[] buffer = new byte[bytes];
- int readBytes = bis.read(buffer);
- if (readBytes != buffer.length) {
- throw new IOException("Entire file not read");
- }
- return buffer;
- } finally {
- if (bis != null) {
- bis.close();
- }
- }
- }
4. 補充,用戶(hù)狀態(tài)的監聽(tīng)
即對方改變頭像,狀態(tài),心情時(shí),更新自己用戶(hù)列表,其實(shí)這里已經(jīng)有smack實(shí)現的監聽(tīng)器
- final Roster roster = Client.getRoster();
- roster.addRosterListener(
- new RosterListener() {
- @Override
- public void entriesAdded(Collection<String> arg0) {
- // TODO Auto-generated method stub
- System.out.println("--------EE:"+"entriesAdded");
- }
- @Override
- public void entriesDeleted(Collection<String> arg0) {
- // TODO Auto-generated method stub
- System.out.println("--------EE:"+"entriesDeleted");
- }
- @Override
- public void entriesUpdated(Collection<String> arg0) {
- // TODO Auto-generated method stub
- System.out.println("--------EE:"+"entriesUpdated");
- }
- @Override
- public void presenceChanged(Presence arg0) {
- // TODO Auto-generated method stub
- System.out.println("--------EE:"+"presenceChanged");
- }
- });
下一篇主要是文件傳輸和接收
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。