使用smack api和openfire服务器添加好友时出现问题 [英] Problem adding buddy with smack api and openfire server

查看:114
本文介绍了使用smack api和openfire服务器添加好友时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手.这给了我很多压力.我需要与smack api和openfire服务器聊天.为此,我的Java代码如下

Hi I am new in Java. And its giving me a lot of stress. I need to chat with smack api and openfire server. For this my java code is 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 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代码登录时,会向其发送可用状态.但是他们无法将自己的状态发送给彼此.相反,他们从好友那里收到两条错误消息.

I run this code twice in my pc. Each for an individual user. I added these two users as friends in openfire by adding rooster. But when they logged in by running the java code above they send there presence as available . But they can't send their presence to each other available. Instead they receives two error messages from their buddy .

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

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

I don't know what is wrong with my code. And i really need to solve this problem very soon. I posted this problem other forums too but can't find any answer. So if anyone can have any solution it would be a very big help. Thank You.

推荐答案

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

In many threads in IgniteRealtime's web you can see that you need to let Smack asynchronously retrieve Roster, so either you change the displayBuddyList() to use a RosterListener instead, or you simply use a Thread.sleep(5000) between the login and the displayBuddyList() function (if you don't want to use a listener, which is recommended) to let it have some time to populate the roster with updated presences.

这篇关于使用smack api和openfire服务器添加好友时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆