`

XMPP——Smack[5]文件传输及离线消息的获取 离线消息和离线文件的实现

 
阅读更多

1.       文件的发送

开一个文件选择框,选中文件后再调用下面的方法

 

  1. public static void sendFile(XMPPConnection connection,  
  2.             String user, File file) throws XMPPException, InterruptedException {  
  3.           
  4.         System.out.println("发送文件开始"+file.getName());  
  5.         FileTransferManager transfer = new FileTransferManager(Client.getConnection());  
  6.         System.out.println("发送文件给: "+user+Client.getServiceNameWithPre());  
  7.         OutgoingFileTransfer out = transfer.createOutgoingFileTransfer(user+Client.getServiceNameWithPre()+"/Smack");//  
  8.           
  9.         out.sendFile(file, file.getName());  
  10.           
  11.         System.out.println("//////////");  
  12.         System.out.println(out.getStatus());  
  13.         System.out.println(out.getProgress());  
  14.         System.out.println(out.isDone());  
  15.           
  16.         System.out.println("//////////");  
  17.           
  18.         System.out.println("发送文件结束");  
  19.     }  

 

 

 

2.       文件接收,必须使用监听

 

 

  1. FileTransferManager transfer = new FileTransferManager(connection);  
  2. transfer.addFileTransferListener(new RecFileTransferListener());  
  3.   
  4. public class RecFileTransferListener implements FileTransferListener {  
  5.   
  6.     public String getFileType(String fileFullName)  
  7.     {  
  8.         if(fileFullName.contains("."))  
  9.         {  
  10.             return "."+fileFullName.split("//.")[1];  
  11.         }else{  
  12.             return fileFullName;  
  13.         }  
  14.           
  15.     }  
  16.       
  17.     @Override  
  18.     public void fileTransferRequest(FileTransferRequest request) {  
  19.         System.out.println("接收文件开始.....");  
  20.         final IncomingFileTransfer inTransfer = request.accept();  
  21.         final String fileName = request.getFileName();  
  22.         long length = request.getFileSize();   
  23.         final String fromUser = request.getRequestor().split("/")[0];  
  24.         System.out.println("文件大小:"+length + "  "+request.getRequestor());  
  25.         System.out.println(""+request.getMimeType());  
  26.         try {   
  27.               
  28.             JFileChooser chooser = new JFileChooser();   
  29.             chooser.setCurrentDirectory(new File("."));   
  30.               
  31.             int result = chooser.showOpenDialog(null);  
  32.               
  33.             if(result==JFileChooser.APPROVE_OPTION)  
  34.             {  
  35.                 final File file = chooser.getSelectedFile();  
  36.                 System.out.println(file.getAbsolutePath());  
  37.                     new Thread(){  
  38.                         public void run()  
  39.                         {  
  40.                         try {  
  41.   
  42.                             System.out.println("接受文件: " + fileName);  
  43.                             inTransfer  
  44.                                     .recieveFile(new File(file  
  45.                                             .getAbsolutePath()  
  46.                                             + getFileType(fileName)));  
  47.   
  48.                             Message message = new Message();  
  49.                             message.setFrom(fromUser);  
  50.                             message.setProperty("REC_SIGN""SUCCESS");  
  51.                             message.setBody("["+fromUser+"]发送文件: "+fileName+"/r/n"+"存储位置: "+file.getAbsolutePath()+ getFileType(fileName));  
  52.                             if (Client.isChatExist(fromUser)) {  
  53.                                 Client.getChatRoom(fromUser).messageReceiveHandler(  
  54.                                         message);  
  55.                             } else {  
  56.                                 ChatFrameThread cft = new ChatFrameThread(  
  57.                                         fromUser, message);  
  58.                                 cft.start();  
  59.                                   
  60.                             }  
  61.                         } catch (Exception e2) {  
  62.                             e2.printStackTrace();  
  63.                         }  
  64.                         }  
  65.                     }.start();  
  66.             }else{  
  67.                   
  68.                 System.out.println("拒绝接受文件: "+fileName);  
  69.                   
  70.                 request.reject();  
  71.                 Message message = new Message();  
  72.                 message.setFrom(fromUser);  
  73.                 message.setBody("拒绝"+fromUser+"发送文件: "+fileName);  
  74.                 message.setProperty("REC_SIGN""REJECT");  
  75.                 if (Client.isChatExist(fromUser)) {  
  76.                     Client.getChatRoom(fromUser)  
  77.                             .messageReceiveHandler(message);  
  78.                 } else {  
  79.                     ChatFrameThread cft = new ChatFrameThread(  
  80.                             fromUser, message);  
  81.                     cft.start();  
  82.                 }  
  83.             }  
  84.               
  85.               
  86.               
  87.               
  88.                
  89.             /* InputStream in = inTransfer.recieveFile(); 
  90.               
  91.              String fileName = "r"+inTransfer.getFileName(); 
  92.               
  93.              OutputStream out = new FileOutputStream(new File("d:/receive/"+fileName)); 
  94.              byte[] b = new byte[512]; 
  95.              while(in.read(b) != -1) 
  96.              { 
  97.                  out.write(b); 
  98.                  out.flush(); 
  99.              } 
  100.               
  101.              in.close(); 
  102.              out.close();*/  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.           
  107.         System.out.println("接收文件结束.....");  
  108.   
  109.     }  
  110.   
  111. }  

 

 

 1.离线消息

  openfire本身是支持离线消息的,不需要进行额外处理,可以用spark测试下

  使用smack,其实他提供了相应的方法

  Class OfflineMessageManager

 

  可以看下描述

 

The OfflineMessageManager helps manage offline messages even before the user has sent an available presence. When a user asks for his offline messages before sending an available presence then the server will not send a flood with all the offline messages when the user becomes online. The server will not send a flood with all the offline messages to the session that made the offline messages request or to any other session used by the user that becomes online.

 

英文退化了点,汗,大意就是,必须在发送在线信息之前去获取离线消息 

 

刚开始没看这个,结果在上线之后,去取,结果。。。。离线消息数量总是为零,囧

 

首先,连接,状态要设为离线

 

  1. ConnectionConfiguration connConfig = new ConnectionConfiguration(serverDomain);  
  2.     
  3.   connConfig.setSendPresence(false); // where connConfig is object of .  
  4.   
  5.      connection = new XMPPConnection(connConfig);  
  6.      connection.connect();  

 

 

然后,登陆

  connection.login(userName, pwd);

 

接着,拿离线消息

 

  1. OfflineMessageManager offlineManager = new OfflineMessageManager(  
  2.                 Client.getConnection());  
  3.         try {  
  4.             Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager  
  5.                     .getMessages();  
  6.   
  7.             System.out.println(offlineManager.supportsFlexibleRetrieval());  
  8.             System.out.println("离线消息数量: " + offlineManager.getMessageCount());  
  9.   
  10.               
  11.             Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();  
  12.               
  13.             while (it.hasNext()) {  
  14.                 org.jivesoftware.smack.packet.Message message = it.next();  
  15.                 System.out  
  16.                         .println("收到离线消息, Received from 【" + message.getFrom()  
  17.                                 + "】 message: " + message.getBody());  
  18.                 String fromUser = message.getFrom().split("/")[0];  
  19.   
  20.                 if(offlineMsgs.containsKey(fromUser))  
  21.                 {  
  22.                     offlineMsgs.get(fromUser).add(message);  
  23.                 }else{  
  24.                     ArrayList<Message> temp = new ArrayList<Message>();  
  25.                     temp.add(message);  
  26.                     offlineMsgs.put(fromUser, temp);  
  27.                 }  
  28.             }  
  29.   
  30.             //在这里进行处理离线消息集合......  
  31.             Set<String> keys = offlineMsgs.keySet();  
  32.             Iterator<String> offIt = keys.iterator();  
  33.             while(offIt.hasNext())  
  34.             {  
  35.                 String key = offIt.next();  
  36.                 ArrayList<Message> ms = offlineMsgs.get(key);  
  37.                 TelFrame tel = new TelFrame(key);  
  38.                 ChatFrameThread cft = new ChatFrameThread(key, null);  
  39.                 cft.setTel(tel);  
  40.                 cft.start();  
  41.                 for (int i = 0; i < ms.size(); i++) {  
  42.                     tel.messageReceiveHandler(ms.get(i));  
  43.                 }  
  44.             }  
  45.               
  46.               
  47.             offlineManager.deleteMessages();  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.         }  

 

 

记得最后要把离线消息删除,即通知服务器删除离线消息

offlineManager.deleteMessages();

否则,下次上了消息还存在

接着,上线

 Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);

 

2.离线文件

 

这个我没实现,汗

主要思想:开发openfire插件,拦截离线文件,将文件存到服务器上,同时在数据库里开一张表,存储文件信息

               当用户上线时,查表,若是有,根据路径,拿了发送

当然,大家可以谷歌下是否有相应的插件,时间紧迫,我倒是没找着

 

 

 

到这里,大概就这些了,对了,还扩展了个视频音频聊天,不过使用的是JMF,点对点的,本来打算使用jingle的,结果连API文档都没找到,晕死

 

就这些

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics