zoukankan      html  css  js  c++  java
  • 使用聊天消息

    使用聊天消息

    背部

    来回发送消息是即时消息的核心。虽然可以作为数据包发送和接收单个消息,但通常更容易将消息字符串视为使用org.jivesoftware.smack.chat2.Chat该类的聊天

    聊天在两个用户之间创建新的消息线程(使用线程ID)。以下代码段演示了如何与用户创建新的聊天,然后向他们发送文本消息:

    // Assume we've created an XMPPConnection name "connection"._
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    chatManager.addIncomingListener(new IncomingChatMessageListener() {
      @Override
      void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
        System.out.println("New message from " + from + ": " + message.getBody());
      }
    });
    EntityBareJid jid = JidCreate.entityBareFrom("jsmith@jivesoftware.com");
    Chat chat = chatManager.chatWith(jid);
    chat.send("Howdy!");
    }
    

    Chat.send(String)方法是一种方便的方法,它创建一个Message对象,使用String参数设置body,然后发送消息。如果您希望在发送消息之前在消息上设置其他值,请使用该Chat.send(Message)方法,如以下代码段所示:

    Message newMessage = new Message();
    newMessage.setBody("Howdy!");
    // Additional modifications to the message Stanza.
    JivePropertiesManager.addProperty(newMessage, "favoriteColor", "red");
    chat.send(newMessage);
    

    您还会在上面的示例中注意到我们指定了IncomingChatMessageListener。每当有新的聊天消息到达时,都会通知收听者。以下代码片段将侦听器用作parrot-bot - 它回显其他用户键入的所有内容。

    // Assume a IncomingChatMessageListener we've setup with a ChatManager
    public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
        // Send back the same text the other user sent us.
        chat.send(message.getBody());
    }
    

    版权所有(C)Jive Software 2002-2008

  • 相关阅读:
    python 语言打印直角三角形的几种方法
    python基础练习,循环、列表、字典、数组
    python命令行运行django项目, can't open file 'manage.py' 问题解决
    python数组列表、字典、拷贝、字符串
    Python输入与循环
    Python实现制度转换(货币,温度,长度)
    Matlab中图论工具箱的应用
    eclipse环境下日志打印输出
    mxGraph上下级节点与连线高亮显示代码
    mxgraph菜单配置及读取菜单节点名称方法
  • 原文地址:https://www.cnblogs.com/endv/p/11420106.html
Copyright © 2011-2022 走看看