我在 localhost 上使用 SMACK api 和 OpenFire 创建了一个简单的客户端。到目前为止,我有两个用户说 X 和 Y。每当有人登录到客户端时,都会有一个发送按钮向用户 Y 发送消息。我已经检查了 XMPP 调试管理器,我可以发送/在那里接收消息。但是,我无法在分配用于显示消息的 JLabel 中显示这些消息。如果用户 Y 点击发送按钮,它会根据功能向自己发送一条消息,该消息会出现在 JLabel 上。但是当用户 X 点击发送按钮时,会收到消息,但是它不会在 Y 的 JLabel 上显示/更新。
有趣的是,在 gtalk 配置下一切正常。
结构如下:
WelcomeUser.java: 是带有用户名和密码提示的欢迎表单。在点击登录按钮时:它将 ChatBoard 类设置为可见并将用户名密码传递给 ChatBoard 的构造函数。代码:
chBoard=new ChatBoard(username.getText(),password.getText());
chBoard.setVisible(true);
setVisible(false);
这是 ChatBoard.java 的代码:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
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 ChatBoard extends JFrame implements MessageListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
static String username, password;
static XMPPConnection connection;
private JTextField textField;
static JLabel board = new JLabel("");
static Chat chat;
public void sendChat() {
try {
chat = connection.getChatManager().createChat(
"userY@my-pc/Smack", this);
chat.sendMessage("Hello, This is " + username);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Create the frame.
*/
public ChatBoard(String user, String pass) {
username = user;
password = pass;
////
///JFrame creation code ommited for this thread.
// ////////////////////////////////////////////
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration config = new ConnectionConfiguration(
"127.0.0.1");
connection = new XMPPConnection(config);
try {
connection.connect();
System.out.println("Connected...");
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Connected. Error :" + e.getMessage());
}
try {
connection.login(username, password);
System.out.println("Logged in...");
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Logged in. Error :" + e.getMessage());
}
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());
}
// /////////////////////////////////
}
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
board.setText(board.getText() + arg1.getBody());
System.out.println(arg1.getFrom() + ":" + arg1.getBody().toString());
}
}
我是新手,如果有任何愚蠢的错误,请原谅我。您的帮助将不胜感激。
杂项。详细信息:本地主机上的 OpenFire 3.7.1 和 SMACK 3.2.2