[英]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.