【发布时间】:2014-02-07 08:15:58
【问题描述】:
我仍在尝试学习如何正确使用 java Smack API,因此我在此处的 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 JabberSmackAPI implements MessageListener{
private XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
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)
{
System.out.println("Received something: " + message.getBody());
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
JabberSmackAPI c = new JabberSmackAPI();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
System.out.println("-----");
System.out.println("Login information:");
System.out.print("username: ");
String login_username = br.readLine();
System.out.print("password: ");
String login_pass = br.readLine();
c.login(login_username, login_pass);
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 服务器正在运行,有两个用户:admin 和 user1。
- 我启用了 Openfire 所需的所有端口也可以正常工作
- Eclipse IDE,当代码示例。
为了运行这个示例,我使用 Eclipse IDE 运行它。我运行这个应用程序 2 次。首先,我使用管理员用户登录,我说我想与 user1@openfire.com 联系。然后我再次以 user1 身份运行示例,我说我想联系 admin@openfire.com。
我有两个样本同时运行。我可以写消息,但另一端的控制台似乎从来没有收到任何东西。我做错了什么?
我也查过其他类似的帖子:
- send and receiving message using smack API
- Unable to display received messages with SMACK api in JAVA
但是,它们也适用于特定情况,似乎对我没有帮助,因为我希望此示例正常运行。我做错了什么?
【问题讨论】:
-
根据你的例子,不要使用“localhost”作为主机
-
你最终解决了这个问题吗?
-
我在 smack 4.1.1 中应用了相同的代码,但给了我很多错误......有人可以帮忙吗?