XMPP——Smack[6]离线消息和离线文件的实现

终篇,三天所学所用,也就这些,如果需要大家要自己去查资料研究研究,功能其实可以很强大的

可惜界面做得不好,一大短处,从大一迄今没整好,主要是个人审美不行,哎

毕业季呀毕业季,明天摆摊卖书,再半月就可能和生活四年的兄弟姐妹说再见,考研考公务员工作的,各奔东西了,我也将南下杭州

感慨,天下无不散之筵席

在此祝所有刚毕业的,学业事业有成吧

不废话了,貌似最近太感性了,理科男,伤不起呀

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.

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

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

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

[java] view plain copy print ?
  1. ConnectionConfiguration connConfig = new ConnectionConfiguration(serverDomain);
  2. connConfig.setSendPresence(false); // where connConfig is object of .
  3. connection = new XMPPConnection(connConfig);
  4. connection.connect();
ConnectionConfiguration connConfig = new ConnectionConfiguration(serverDomain); connConfig.setSendPresence(false); // where connConfig is object of . connection = new XMPPConnection(connConfig); connection.connect();

然后,登陆

connection.login(userName, pwd);

接着,拿离线消息

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

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

offlineManager.deleteMessages();

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

接着,上线

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

2.离线文件

这个我没实现,汗

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

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

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

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

就这些

你可能感兴趣的:(smack,IM,XMPP)