我在本地主機上使用SMACK api和OpenFire做了一個簡單的客戶端。到目前爲止,我有兩個用戶說X和Y.每當有人登錄到客戶端,就會有一個發送按鈕向用戶Y發送消息。我已經檢查了XMPP調試管理器,並且能夠發送/在那裏接收消息。但是,我無法在我已分配用於顯示消息的JLabel中顯示這些消息。如果用戶Y點擊發送按鈕,按照功能它發送一條消息給自己,並在JLabel上發送。但是,當用戶X點擊發送按鈕時,會收到消息,但不會在Y的JLabel上顯示/更新。無法在JAVA中使用SMACK api顯示收到的消息
有趣的是,一切工作,因爲它應該與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(
"[email protected]/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
您好,非常感謝您的回覆。我試過你給的代碼,同樣的事情仍然存在。我只獲得用戶Y點擊發送按鈕的控制檯輸出。有趣的是,在與gtalk客戶端進行溝通時,一切工作都很順利: - /我無法弄清楚什麼是錯誤的 – harsh8888
查看http://www.igniterealtime.org/builds/smack/docs/latest的底部/documentation/messaging.html。對於來自其他用戶的聊天室,您必須重寫ChatManagerListener –
嗨alex。謝謝你的幫助。它現在正在工作:-) – harsh8888