【问题标题】:Problem adding buddy with smack api and openfire server使用 smack api 和 openfire 服务器添加好友时出现问题
【发布时间】:2011-03-17 18:46:39
【问题描述】:

您好,我是 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

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

【问题讨论】:

  • 有人请帮忙。我需要快速解决这个问题:(

标签: java chat openfire smack


【解决方案1】:

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

【讨论】:

  • 感谢您的回复:)我已经找到答案并解决了:D
  • @kalkin 我有同样的问题,我正在搜索过去 4 天的解决方案。你能帮帮我吗??
  • @Tahlil 请给我推荐一个关于 XMPP/SMACK/OPENFIRE 的好教程。我想开发一个简单的聊天应用。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 2015-07-29
  • 2013-09-07
  • 2011-10-01
相关资源
最近更新 更多