java - send and receiving message using smack API -


i have setup open fire(jabber server) on local machine 2 user testuser1 , testuser2 .using spark client both users perform chat without issue,it's nice.

openfire ip -192.168.1.65

i want use smack api(3.3.0) send , receiving message. have write sender side code send message(with testuser1) , tested spark client(with testuser2) message received on testuser2 side,but when try java code receive sender message ,i not able receive 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 messages 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 messages standard out.                  system.out.println("received message: " + message);              }          });          chat.sendmessage("howdy test2!");           while (true) {         thread.sleep(50);     }          }  } 

please me , suggest if following wrong approach.

thanks

i had similar problem, after following tutorial here (http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html) , found:

when create chat, chat user want connect (eg in case "user1@gbd038"). works fine if user1 using gui client such spark (which presumably has built-in support and/or error handling this), , user1 receive message. process attaches messagelistener chat associated "user1@gbd038". however, when reply spark user1, chat smack receives coming through complete location tag, eg:

received message 'hi' user1@gbd038/spark 2.6.3 

so creates new chat application not listening (and therefore application not receive / print out). have found 2 ways solve problem:

  1. use location tag when starting conversation (although doesn't seem scalable or robust):

    xmppmanager.sendmessage("hello mate", "user1@gbd038/spark 2.6.3");

  2. as robin suggested, use chatmanagerlistener (which create new chat when receiving reply user1, can forwarded messagelistener):

    chatmanager = connection.getchatmanager(); messagelistener = new mymessagelistener();  chatmanagerlistener = new chatmanagerlistener() {     @override     public void chatcreated(chat chat, boolean createdlocally) {         chat.addmessagelistener(messagelistener);     } }; chatmanager.addchatlistener(chatmanagerlistener); 

hope helps in same position!


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -