使用Java Smack API发送和接收消息不能用于示例

时间:2014-01-17 20:37:42

标签: java eclipse xmpp smack

我仍然在努力学习如何正确使用java Smack API,所以我在这里的java编程论坛中关注了一个迷你教程:

然后我将代码更改为我的需求。代码发布在下面:

import java.util.*;
import java.io.*;

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

public class JabberSmackAPI implements MessageListener{

    private XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException
    {
        ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
        connection = new XMPPConnection(config);

        connection.connect();
        connection.login(userName, password);
    }

    public void sendMessage(String message, String to) throws XMPPException
    {
        Chat chat = connection.getChatManager().createChat(to, this);
        chat.sendMessage(message);
    }

    public void displayBuddyList()
    {
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();

        System.out.println("\n\n" + entries.size() + " buddy(ies):");
        for(RosterEntry r:entries)
        {
            System.out.println(r.getUser());
        }
    }

    public void disconnect()
    {
        connection.disconnect();
    }

    public void processMessage(Chat chat, Message message)
    {
        System.out.println("Received something: " + message.getBody());
        if(message.getType() == Message.Type.chat)
            System.out.println(chat.getParticipant() + " says: " + message.getBody());
    }

    public static void main(String args[]) throws XMPPException, IOException
    {
        // declare variables
        JabberSmackAPI c = new JabberSmackAPI();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String msg;


        // turn on the enhanced debugger
        XMPPConnection.DEBUG_ENABLED = true;


        // Enter your login information here
        System.out.println("-----");
        System.out.println("Login information:");

        System.out.print("username: ");
        String login_username = br.readLine();

        System.out.print("password: ");
        String login_pass = br.readLine();

        c.login(login_username, login_pass);

        c.displayBuddyList();

        System.out.println("-----");

        System.out.println("Who do you want to talk to? - Type contacts full email address:");
        String talkTo = br.readLine();

        System.out.println("-----");
        System.out.println("All messages will be sent to " + talkTo);
        System.out.println("Enter your message in the console:");
        System.out.println("-----\n");

        while( !(msg=br.readLine()).equals("bye"))
        {
            c.sendMessage(msg, talkTo);
        }

        c.disconnect();
        System.exit(0);
    }

}

为了运行此代码,我进行了以下设置:

  1. 运行Openfire服务器,有两个用户:admin和user1。
  2. 我启用了Openfire应该要求的所有端口
  3. Eclipse IDE,代码示例时。
  4. 要运行此示例,我使用Eclipse IDE运行它。我运行这个应用程序2次。首先我用admin用户登录,我说我想联系user1@openfire.com。然后我再次运行示例,作为user1,我说我想联系admin@openfire.com。

    我有两个样本同时运行。我可以写消息,但是另一端的控制台似乎从未接收过任何东西。我做错了什么?

    我还检查了其他类似帖子:

    但是,它们也适用于特定情况,似乎对我没有帮助,因为我希望这个示例正常运行。我做错了什么?

3 个答案:

答案 0 :(得分:3)

@Flame_Phoenix您的示例代码正常工作,我查看了我的ejabberd服务器。我修改了代码

确保您添加了这些库

  • 的HttpClient-4.1.3.jar
  • 的HttpCore-4.1.4.jar
  • jstun.jar
  • xpp3-1.1.4c.jar

版本可能会被更改

import java.util.*;
import java.io.*;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;

public class JabberSmackAPI implements MessageListener {

private XMPPConnection connection;
private final String mHost = "yourserver.com"; // server IP address or the
                                                // host

public void login(String userName, String password) throws XMPPException {
    String service = StringUtils.parseServer(userName);
    String user_name = StringUtils.parseName(userName);

    ConnectionConfiguration config = new ConnectionConfiguration(mHost,
            5222, service);

    config.setSendPresence(true);
    config.setDebuggerEnabled(false);

    connection = new XMPPConnection(config);
    connection.connect();
    connection.login(user_name, password);
}

public void sendMessage(String message, String to) throws XMPPException {
    Chat chat = connection.getChatManager().createChat(to, this);
    chat.sendMessage(message);
}

public void displayBuddyList() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();

    System.out.println("\n\n" + entries.size() + " buddy(ies):");
    for (RosterEntry r : entries) {
        System.out.println(r.getUser());
    }
}

public void disconnect() {
    connection.disconnect();
}

public void processMessage(Chat chat, Message message) {
    System.out.println("Received something: " + message.getBody());
    if (message.getType() == Message.Type.chat)
        System.out.println(chat.getParticipant() + " says: "
                + message.getBody());
}

public static void main(String args[]) throws XMPPException, IOException {
    // declare variables
    JabberSmackAPI c = new JabberSmackAPI();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String msg;

    // turn on the enhanced debugger
    XMPPConnection.DEBUG_ENABLED = true;

    // Enter your login information here
    System.out.println("-----");
    System.out.println("Login information:");

    System.out.print("username: ");
    String login_username = br.readLine();

    System.out.print("password: ");
    String login_pass = br.readLine();

    c.login(login_username, login_pass);

    c.displayBuddyList();

    System.out.println("-----");

    System.out
            .println("Who do you want to talk to? - Type contacts full email address:");
    String talkTo = br.readLine();

    System.out.println("-----");
    System.out.println("All messages will be sent to " + talkTo);
    System.out.println("Enter your message in the console:");
    System.out.println("-----\n");

    while (!(msg = br.readLine()).equals("bye")) {
        c.sendMessage(msg, talkTo);
    }

    c.disconnect();
    System.exit(0);
}

}

答案 1 :(得分:2)

我尝试了这个例子,只有smack和smackx添加到我对pom的依赖中,它运行正常。

<dependency>
    <groupId>org.igniterealtime.smack</groupId>
    <artifactId>smack</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>org.igniterealtime.smack</groupId>
    <artifactId>smackx</artifactId>
    <version>3.3.1</version>
</dependency>

为此,您还需要添加一个存储库

<repository>
    <id>nexus.opencastProject.org</id>
    <url>http://repository.opencastproject.org/nexus/content/repositories/public</url>
    <name>Opencast Nexus All Public Project Repository</name>
</repository>

希望这有帮助。

答案 2 :(得分:1)

对于Smack 4.1,MessageListener接口变为ChatMessageListener。 MessageListener仍然存在,但它有一个不同的接口契约(processMessage只接受一个消息,而不是一个聊天)。