Package org.jivesoftware.smack

Examples of org.jivesoftware.smack.Chat


        ChatManager chatManager = connection.getChatManager();

        if (LOG.isTraceEnabled()) {
            LOG.trace("Looking for existing chat instance with thread ID " + endpoint.getChatId());
        }
        Chat chat = chatManager.getThreadChat(endpoint.getChatId());
        if (chat == null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Creating new chat instance with thread ID " + endpoint.getChatId());
            }
            chat = chatManager.createChat(getParticipant(), endpoint.getChatId(), new MessageListener() {
                public void processMessage(Chat chat, Message message) {
                    // not here to do conversation
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Received and discarding message from " + getParticipant() + " : " + message.getBody());
                    }
                }
            });
        }

        Message message = null;
        try {
            message = new Message();
            message.setTo(getParticipant());
            message.setThread(endpoint.getChatId());
            message.setType(Message.Type.normal);

            endpoint.getBinding().populateXmppMessage(message, exchange);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Sending XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message.getBody());
            }
            chat.sendMessage(message);
        } catch (XMPPException xmppe) {
            throw new RuntimeExchangeException("Cannot send XMPP message: to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
                    + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, xmppe);
        } catch (Exception e) {
            throw new RuntimeExchangeException("Cannot send XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
View Full Code Here


   *
   * @param jid
   *            一个指定的jid
   * */
  private Chat creatChat(String jid) {
    Chat chat = chatManager.createChat(jid, new MessageListener() {
      @Override
      public void processMessage(Chat chat, Message msg) {
        // 应该放入“未读消息”中
//        Util.showDebugMsg("\n"
//                + chat.getParticipant()
View Full Code Here

     * This is a simple test to use with a XMPP client and check if the client receives the message
     * 1. User_1 will send a message with formatted text (XHTML) to user_2
     */
    public void testSendSimpleXHTMLMessage() {
  // User1 creates a chat with user2
  Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);

  // User1 creates a message to send to user2
  Message msg = new Message();
  msg.setSubject("Any subject you want");
  msg.setBody("Hey John, this is my new green!!!!");
  // Create a XHTMLExtension Package and add it to the message
  XHTMLExtension xhtmlExtension = new XHTMLExtension();
  xhtmlExtension.addBody(
  "<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
  msg.addExtension(xhtmlExtension);

  // User1 sends the message that contains the XHTML to user2
  try {
      chat1.sendMessage(msg);
      Thread.sleep(200);
  }
  catch (Exception e) {
      fail("An error occured sending the message with XHTML");
  }
View Full Code Here

     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
     * something is wrong
     */
    public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
  // Create a chat for each connection
  Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
  final PacketCollector chat2 = getConnection(1).createPacketCollector(
    new ThreadFilter(chat1.getThreadID()));

  // User1 creates a message to send to user2
  Message msg = new Message();
  msg.setSubject("Any subject you want");
  msg.setBody("Hey John, this is my new green!!!!");
  // Create a XHTMLExtension Package and add it to the message
  XHTMLExtension xhtmlExtension = new XHTMLExtension();
  xhtmlExtension.addBody(
  "<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
  msg.addExtension(xhtmlExtension);

  // User1 sends the message that contains the XHTML to user2
  try {
      chat1.sendMessage(msg);
  }
  catch (Exception e) {
      fail("An error occured sending the message with XHTML");
  }
  Packet packet = chat2.nextResult(2000);
View Full Code Here

     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
     * something is wrong
     */
    public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
  // Create a chat for each connection
  Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
  final PacketCollector chat2 = getConnection(1).createPacketCollector(
    new ThreadFilter(chat1.getThreadID()));

  // Create a Listener that listens for Messages with the extension
  //"http://jabber.org/protocol/xhtml-im"
  // This listener will listen on the conn2 and answer an ACK if everything is ok
  PacketFilter packetFilter =
      new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
  PacketListener packetListener = new PacketListener() {
      @Override
      public void processPacket(Packet packet) {

      }
  };
  getConnection(1).addPacketListener(packetListener, packetFilter);

        // User1 creates a message to send to user2
        Message msg = new Message();
        msg.setSubject("Any subject you want");
        msg.setBody(
                "awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
        // Create an XHTMLExtension and add it to the message
        XHTMLExtension xhtmlExtension = new XHTMLExtension();
        xhtmlExtension.addBody(
                "<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridicula es el espantajo de mentes pequenas.</p></blockquote></body>");
        xhtmlExtension.addBody(
                "<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
        msg.addExtension(xhtmlExtension);

  // User1 sends the message that contains the XHTML to user2
  try {
      bodiesSent = xhtmlExtension.getBodiesCount();
      bodiesReceived = 0;
      chat1.sendMessage(msg);
  }
  catch (Exception e) {
      fail("An error occured sending the message with XHTML");
  }
  Packet packet = chat2.nextResult(2000);
View Full Code Here

        field.setLabel("How old are you?");
        field.setType(FormField.TYPE_TEXT_SINGLE);
        formToSend.addField(field);

        // Create the chats between the two participants
        Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
        PacketCollector collector = getConnection(0).createPacketCollector(
                new ThreadFilter(chat.getThreadID()));
        PacketCollector collector2 = getConnection(1).createPacketCollector(
                new ThreadFilter(chat.getThreadID()));

        Message msg = new Message();
        msg.setBody("To enter a case please fill out this form and send it back to me");
        msg.addExtension(formToSend.getDataFormToSend());

        try {
            // Send the message with the form to fill out
            chat.sendMessage(msg);

            // Get the message with the form to fill out
            Message msg2 = (Message)collector2.nextResult(2000);
            assertNotNull("Messge not found", msg2);
            // Retrieve the form to fill out
View Full Code Here

                    }
                }
            });

            // Start a private chat with another participant           
            Chat chat = muc2.createPrivateChat(room + "/testbot", null);
            PacketCollector collector = chat.createCollector();
            chat.sendMessage("Hello there");

            Message response = (Message) collector.nextResult(2000);
            assertNotNull("No response", response);
            assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
            assertEquals("Body of response is incorrect", "ACK", response.getBody());
View Full Code Here

        try {
            Thread.sleep(500);

            // User1 sends some messages to User2 which is not available at the moment
            Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
            chat.sendMessage("Test 1");
            chat.sendMessage("Test 2");

            Thread.sleep(500);

            // User2 checks the number of offline messages
            OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
View Full Code Here

        try {
            Thread.sleep(500);

            // User1 sends some messages to User2 which is not available at the moment
            Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
            chat.sendMessage("Test 1");
            chat.sendMessage("Test 2");

            Thread.sleep(500);

            // User2 checks the number of offline messages
            OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
View Full Code Here

      XMPPConnection conn = XMPPConnect.getConnection();

      if (attachmentUrl == null) {
        // send a normal message without an attachment
        ChatManager chatmanager = ChatManager.getInstanceFor(conn);
        Chat newChat = chatmanager.createChat(to, new MessageListener() {
          public void processMessage(Chat chat, Message message) {
            logger.debug("Received message on XMPP: {}", message.getBody());
          }
        });
        try {
          newChat.sendMessage(message);
          logger.debug("Sent message '{}' to '{}'.", message, to);
          success = true;
        } catch (XMPPException e) {
          logger.error("Error sending message '{}'", message, e);
        } catch (NotConnectedException e) {
View Full Code Here

TOP

Related Classes of org.jivesoftware.smack.Chat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.