Java 使用 smack API 发送和接收消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18417216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:27:14  来源:igfitidea点击:

send and receiving message using smack API

javaxmppopenfiresmack

提问by Sameek Mishra

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.

我已经在本地机器上设置了我的 open fire(jabber 服务器),有两个用户 testuser1 和 testuser2 。使用 Spark 客户端,两个用户都可以毫无问题地进行聊天,这很好。

openfire IP -192.168.1.65

开火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.

我想使用 smack API(3.3.0) 来发送和接收消息。我已经编写了发送方代码来发送消息(使用 testuser1)并使用在 testuser2 端接收到的 Spark 客户端(使用 testuser2)消息进行测试,但是当我尝试使用 java 代码接收发送方消息时,我无法接收那些发布消息。

Sender.java

发件人.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

接收器.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

谢谢

回答by Robin

You are creating a chat and sending a chat message from both ends but not listening for a chat from either. Use a ChatManagerListener to listen for incoming chats from other clients.

您正在创建聊天并从两端发送聊天消息,但不收听任何一方的聊天。使用 ChatManagerListener 侦听来自其他客户端的传入聊天。

回答by Matt

I had a similar problem, after following the tutorial here (http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html) and this is what I found:

在遵循此处的教程(http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html)后,我遇到了类似的问题,这就是我发现的:

When you create the chat, you chat the user you want to connect to (eg in my case "user1@gbd038"). This works fine if user1 is using a GUI client such as Spark (which presumably has built-in support and/or error handling for this), and user1 will receive the message. This process attaches the messageListener to a chat now associated with "user1@gbd038". However, when I reply from Spark as user1, the chat that smack receives is actually coming through complete with the location tag, eg:

创建聊天时,您与要连接的用户聊天(例如,在我的情况下为“user1@gbd038”)。如果 user1 使用的是 GUI 客户端,例如 Spark(它可能具有内置的支持和/或错误处理),这可以正常工作,并且 user1 将收到消息。此过程将 messageListener 附加到现在与“user1@gbd038”关联的聊天中。但是,当我以 user1 的身份从 Spark 回复时,smack 收到的聊天实际上是通过位置标签完成的,例如:

Received message 'hi' from user1@gbd038/Spark 2.6.3

So it creates a new chat that the application is not listening for (and therefore your application will not receive / print out). I have found two ways to solve this problem:

因此,它会创建一个应用程序未侦听的新聊天(因此您的应用程序将不会接收/打印出来)。我找到了两种方法来解决这个问题:

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

    xmppManager.sendMessage("Hello mate", "user1@gbd038/Spark 2.6.3");

  2. as Robin suggested, use a ChatManagerListener (which will create a new chat when receiving the reply from user1, which can be forwarded to the messageListener):

    chatManager = connection.getChatManager();
    messageListener = new MyMessageListener();
    
    chatManagerListener = new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally) {
            chat.addMessageListener(messageListener);
        }
    };
    chatManager.addChatListener(chatManagerListener);
    
  1. 在开始对话时使用位置标签(尽管这看起来不是很可扩展或健壮):

    xmppManager.sendMessage("Hello mate", "user1@gbd038/Spark 2.6.3");

  2. 正如 Robin 建议的那样,使用 ChatManagerListener(它会在收到来自 user1 的回复时创建一个新的聊天,可以转发到 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 that helps someone in the same position!

希望能帮助到相同情况的人!

回答by Anuj Dhiman

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

有关完整代码示例,请访问如何使用 smack api 发送消息