XMPP和即时通讯内容学习(二)

继续接着上面描述的内容写,上一篇内容我们是放松了一个消息给他人,那么我们怎么监听他人返回给我们消息呢?

1、创建消息监听,监听消息队列的内容:

[java] view plaincopyprint?[图片上传失败...(image-581a0b-1551512336631)] [图片上传失败...(image-ac5afb-1551512336631)]

  1. XMPPConnection.DEBUG_ENABLED = false;

  2. AccountManager accountManager;

  3. final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(

  4. "127.0.0.1", Integer.parseInt("5222"), "127.0.0.1");

  5. // 允许自动连接

  6. connectionConfig.setReconnectionAllowed(true);

  7. connectionConfig.setSendPresence(true);

  8. Connection connection = new XMPPConnection(connectionConfig);

  9. try {

  10. connection.connect();// 开启连接

  11. accountManager = connection.getAccountManager();// 获取账户管理类

  12. } catch (XMPPException e) {

  13. throw new IllegalStateException(e);

  14. }

  15. connection.login("sujingbo", "123456");

  16. System.out.println(connection.getUser());

上面是继续昨天的代码,与openfire建立连接,接下来就是监听消息队列了:

[java] view plaincopyprint?[图片上传失败...(image-11c9f7-1551512336631)] [图片上传失败...(image-24c55b-1551512336631)]

  1. //创建用户监听,监听消息队列,实现互动聊天。

  2. ChatManager chatmanager = connection.getChatManager();

  3. Chat newChat = chatmanager.createChat("[email protected]", new MessageListener() {

  4. public void processMessage(Chat chat, Message message) {

  5. if (message.getBody() != null) {

  6. System.out.println("Received from 【"

    • message.getFrom() + "】 message: "
    • message.getBody());
  7. }

  8. }

  9. });

  10. Scanner input = new Scanner(System.in);

  11. while (true) {

  12. String message = input.nextLine();

  13. newChat.sendMessage(message);

  14. }

运行上述代码,然后在控制台直接输入 nihao,这时spark客户端就会收到你所发出的消息,并可以进行回复,控制台和spark的截图如下:

[图片上传失败...(image-eab3f9-1551512336635)]

[图片上传失败...(image-adbf72-1551512336635)]

这样互动就建立起来了,我这道这边这一步的时候很兴奋(初学者吗,都这样,大家见谅)!

2、发送系统通知

大家都知道,作为一款即时通许软件都会有一个系统发送通知,通知所有联系人,废话不多话,代码如下:

[java] view plaincopyprint?[图片上传失败...(image-92a953-1551512336631)] [图片上传失败...(image-89368d-1551512336631)]

  1. XMPPConnection.DEBUG_ENABLED = false;

  2. // AccountManager accountManager;

  3. final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(

  4. "127.0.0.1", Integer.parseInt("5222"), "127.0.0.1");

  5. // 允许自动连接

  6. connectionConfig.setReconnectionAllowed(true);

  7. connectionConfig.setSendPresence(true);

  8. Connection connection = new XMPPConnection(connectionConfig);

  9. try {

  10. connection.connect();// 开启连接

  11. // accountManager = connection.getAccountManager();// 获取账户管理类

  12. } catch (XMPPException e) {

  13. throw new IllegalStateException(e);

  14. }

  15. connection.login("sujingbo", "123456");

  16. System.out.println(connection.getUser());

  17. //创建通知内容并发送

  18. Message newmsg = new Message();

  19. newmsg.setTo("[email protected]");

  20. newmsg.setSubject("重要通知");

  21. newmsg.setBody("今天下午2点60分有会!");

  22. newmsg.setType(Message.Type.headline);// normal支持离线

  23. connection.sendPacket(newmsg);

  24. connection.disconnect();

上面内容不懂,注意这段代码中多了一个Message对象,学习android的朋友肯定都这,这时一个消息的载体,可以携带很多内容,在这也不例外,设置发送对象,发送主题以及发送内容等;直接运行代码;你在spark端就会收到一

你可能感兴趣的:(XMPP和即时通讯内容学习(二))