简体   繁体   English

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

[英]Sending and receiving message using Java Smack API not working on example

I am still trying to learn how to properly work with the java Smack API, so I followed a mini-tutorial in a java programming forum here:我仍在尝试学习如何正确使用 java Smack API,所以我在这里遵循了 java 编程论坛中的一个迷你教程:

I then changed the code to my needs.然后我根据我的需要更改了代码。 The code is posted below:代码贴在下面:

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

}

In order to run this code, I have the following setup:为了运行此代码,我进行了以下设置:

  1. An Openfire server running, with two users: admin and user1.运行 Openfire 服务器,有两个用户:admin 和 user1。
  2. I enabled all the ports Openfire should require to work as well我启用了 Openfire 工作所需的所有端口
  3. Eclipse IDE, when the code sample. Eclipse IDE,当代码示例。

To run this sample, I run it using Eclipse IDE.为了运行这个示例,我使用 Eclipse IDE 运行它。 I run this application 2 times.我运行这个应用程序 2 次。 First I login with the admin user, and I say I want to contact with user1@openfire.com.首先我用admin用户登录,我说我想联系user1@openfire.com。 Then I run the sample again, as user1, and I say I want to contact with admin@openfire.com.然后我再次运行示例,作为用户 1,我说我想联系 admin@openfire.com。

I have the two samples running at the same time.我有两个样本同时运行。 I can write messages, but the console in the other end never seems to be receiving anything.我可以写消息,但另一端的控制台似乎从未收到任何消息。 What am I doing wrong?我究竟做错了什么?

I have also checked other similar posts:我还检查了其他类似的帖子:

However, they are also for specific cases and do not seem to help me, as I would like to have this sample running properly.但是,它们也适用于特定情况,似乎对我没有帮助,因为我希望此示例正常运行。 What am I doing wrong?我究竟做错了什么?

@Flame_Phoenix your sample code is working, I checked with my ejabberd server. @Flame_Phoenix 您的示例代码正在运行,我检查了我的 ejabberd 服务器。 I modified the code我修改了代码

Make sure that you added these libraries确保您添加了这些库

  • httpclient-4.1.3.jar httpclient-4.1.3.jar
  • httpcore-4.1.4.jar httpcore-4.1.4.jar
  • jstun.jar jstun.jar
  • xpp3-1.1.4c.jar xpp3-1.1.4c.jar

versions might be changed版本可能会改变

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

}

I tried the example with only smack and smackx added in my dependency of pom and it is working fine.我尝试了在我的 pom 依赖项中仅添加 smack 和 smackx 的示例,并且它工作正常。

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

For this you will need to add one repository too为此,您还需要添加一个存储库

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

Hope this helps.希望这可以帮助。

For Smack 4.1, the MessageListener interface became ChatMessageListener.对于 Smack 4.1,MessageListener 接口变成了 ChatMessageListener。 MessageListener still exists, but it has a different interface contract (processMessage takes only a Message, not a Chat). MessageListener 仍然存在,但它有一个不同的接口契约(processMessage 只接受一个 Message,而不是一个 Chat)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM