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

  • 相关阅读:
    Service Location Protocol SLP
    [http 1.1] M-POST
    安装 wbemcli
    [http 1.1] M-POST w3
    [CODEVS 1288]埃及分数
    [NOIp 2013]货车运输
    [测试题]gentree
    [USACO 07NOV]Cow Relays
    [USACO 13DEC]Vacation Planning(gold)
    [CODEVS 2495]水叮当的舞步
  • 原文地址:https://www.cnblogs.com/endv/p/11420106.html
Copyright © 2011-2022 走看看