Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send and receiving message using smack API

I have setup my open fire(jabber server) on local machine with two user testuser1 and testuser2 .using Spark client both users perform chat without any issue,it's nice.

openfire IP -192.168.1.65

I want to use smack API(3.3.0) for send and receiving message. i have write sender side code to send message(with testuser1) and tested with Spark client(with testuser2) message received on testuser2 side,but when i try with java code to receive sender message ,i am not able to receive those publish messages.

Sender.java

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;

public class Sender 
{

    public static void main(String a[]) throws XMPPException, InterruptedException
    {
         XMPPConnection connection = new XMPPConnection("192.168.1.65");  
         System.out.println(connection);
         connection.connect();
         connection.login("testuser1", "test123");



         Chat chat = connection.getChatManager().createChat("testuser2@sameek", new MessageListener() {

             public void processMessage(Chat chat, Message message) {
                 // Print out any messages we get back to standard out.
                 System.out.println("Received message: " + message);
             }
         });
         chat.sendMessage("Howdy test1!");

         while (true) {
        Thread.sleep(50);
    }





    }

}

Receiver.java

  import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;






public class Receiver
{

    public static void main(String a[]) throws XMPPException,, InterruptedException
    {
         XMPPConnection connection = new XMPPConnection("192.168.1.65");  
         System.out.println(connection);
         connection.connect();
         connection.login("testuser2", "test123");



         Chat chat = connection.getChatManager().createChat("testuser1@sameek", new MessageListener() {

             public void processMessage(Chat chat, Message message) {
                 // Print out any messages we get back to standard out.
                 System.out.println("Received message: " + message);
             }
         });
         chat.sendMessage("Howdy test2!");

         while (true) {
        Thread.sleep(50);
    }





    }

}

please help me and suggest if i am following wrong approach.

Thanks

like image 610
Sameek Mishra Avatar asked Aug 24 '13 09:08

Sameek Mishra


Video Answer


1 Answers

Use below code for sending and receiving message

@Override
         public void processMessage(Chat chat, Message message) {
             // Print out any messages we get back to standard out.
             System.out.println("Received message: " + message);
         }

     });

chat.sendMessage("How are you dear !!");
System.out.println(" Send Message succesfully");

For full code example visit How to send message using smack api

like image 169
Anuj Dhiman Avatar answered Sep 20 '22 23:09

Anuj Dhiman