zoukankan      html  css  js  c++  java
  • XMPP——Smack[6]离线消息和离线文件的实现 (转) CVT

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

    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文档都没找到,晕死

  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/openfire/p/3029281.html
Copyright © 2011-2022 走看看