1

嗨,我是 Java 新手。它给了我很大的压力。我需要与 smack api 和 openfire 服务器聊天。为此,我的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 RunJabberSmackAPI implements MessageListener{

    XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException {
        ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1 ",5222,"localhost");
        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) {
      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
      RunJabberSmackAPI c = new RunJabberSmackAPI();
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String msg;

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

      // Enter your login information here
      c.login("admin", "admin");    // I created this user with openfire.

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

我在我的电脑上运行此代码两次。每个用于单个用户。我通过添加公鸡将这两个用户添加为openfire中的朋友。但是,当他们通过运行上面的 java 代码登录时,他们会在那里发送存在 as available 。但是他们不能将他们的存在发送给彼此可用。相反,他们从好友那里收到两条错误消息。

First error message :  www.freeimagehosting.net/image.php?eac15f606a.jpg
Second error message : www.freeimagehosting.net/image.php?b827058d07.jpg

我不知道我的代码有什么问题。我真的需要尽快解决这个问题。我也在其他论坛发布了这个问题,但找不到任何答案。因此,如果任何人都可以有任何解决方案,那将是一个很大的帮助。谢谢你。

4

1 回答 1

0

在 IgniteRealtime 网络中的许多线程中,您可以看到您需要让 Smack 异步检索名册,因此您可以将 displayBuddyList() 更改为使用 RosterListener,或者您只需在登录名和 displayBuddyList 之间使用 Thread.sleep(5000) () 函数(如果您不想使用侦听器,建议使用),让它有时间用更新的存在填充名册。

于 2011-07-14T07:16:40.283 回答