繁体   English   中英

使用smack api和openfire服务器添加好友时出现问题

[英]Problem adding buddy with smack api and openfire server

嗨,我是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);
    }
}

我在我的PC中运行了两次此代码。 每个针对单个用户。 我通过添加公鸡将这两个用户添加为openfire的朋友。 但是,当他们通过运行上面的Java代码登录时,会向其发送可用状态。 但是他们无法将自己的状态发送给彼此。 相反,他们从好友那里收到两条错误消息。

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

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

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

暂无
暂无

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

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