使用smack API发送和接收消息 [英] send and receiving message using smack API

查看:349
本文介绍了使用smack API发送和接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在本地计算机上用两个用户testuser1和testuser2设置了我的开火(jabber服务器).使用Spark客户端,两个用户都可以毫无问题地进行聊天,

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

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

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.

谢谢

推荐答案

在按照此处的说明进行操作后,我遇到了类似的问题(

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:

在创建聊天时,您要与要连接的用户聊天(例如,在我的情况下为"user1 @ gbd038").如果user1使用GUI客户端(例如Spark)(可能对此具有内置支持和/或错误处理),则此方法很好用,并且user1将接收到该消息.此过程将messageListener附加到现在与"user1 @ gbd038"关联的聊天.但是,当我以用户1的身份从Spark答复时,smack收到的聊天实际上是通过位置标记完成的,例如:

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:

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. 在开始对话时使用location标记(尽管这似乎不太可扩展或不可靠):

  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");

就像罗宾建议的那样,使用ChatManagerListener(当接收到来自user1的回复时可以创建新的聊天,可以将其转发到messageListener):

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);

希望能帮助处于同一位置的人!

Hope that helps someone in the same position!

这篇关于使用smack API发送和接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆