我开发了一个简单的聊天客户端,它可以发送和接收消息以及用户在 2 个用户(userx 和 usery)之间输入 CHatState 通知。在调试窗口中,我看到包含正文或带有聊天状态的数据包。所有数据包都被正确记录。
问题 1:但我的窗口同时显示在聊天(JLabel)中。正因为如此,每当一个用户开始打字时,另一个用户会在他的窗口中看到一条空消息,这实际上是一个包含聊天状态通知的数据包,没有任何正文。我尝试了各种条件,但都失败了。我尝试了以下荒谬的 if 条件,但它显示空数据包。
if (message != null || message.getBody().isEmpty() == false
|| message.getBody() != null
|| message.getBody().toString().compareTo("null") == 1
&& message.getError() == null)
问题 2:我无法记录发送的通知聊天状态。我的代码如下:
import java.applet.Applet;
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("<html>");
static Chat chat;
String message;
static ChatState status;
static String to;
static boolean flag = false;
JLabel typingStat;
public class typingStatus implements ChatStateListener {
@Override
public void stateChanged(Chat arg0, ChatState arg1) {
// TODO Auto-generated method stub
System.out.println(arg1.name());
}
@Override
public void processMessage(Chat arg0, Message arg1) {
// TODO Auto-generated method stub
System.out.println(arg1.getBody());
}
}
public void sendChat(String msg) {
try {
to = "harsh00008";
if (username.compareTo(to) == 0)
to = "usery@xyz";
else
to = "userx@xyz";
chat = connection.getChatManager().createChat(to, this);
chat.sendMessage(msg);
} catch (XMPPException e) {
e.printStackTrace();
}
}
public void changeStatus() {
// TODO Auto-generated method stub
if (username.compareTo("harsh00008") == 0)
chat = connection.getChatManager().createChat("test@prc.p1.im",
this);
else
chat = connection.getChatManager().createChat(
"harsh00008@prc.p1.im", this);
if (textField.getText().isEmpty() == false && flag == false) {
try {
ChatStateManager.getInstance(connection).setCurrentState(
ChatState.composing, ChatBoard.chat);
flag = true;
} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
if (textField.getText().isEmpty() == true && flag == true) {
try {
ChatStateManager.getInstance(connection).setCurrentState(
ChatState.paused, ChatBoard.chat);
flag = false;
} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public ChatBoard(String user, String pass) {
setVisible(true);
username = user;
password = pass;
//JFRAME CREATION CODE OMITTED
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
changeStatus();
}
});
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (textField.getText().isEmpty() == false) {
sendChat(textField.getText().toString());
board.setText(board.getText() + "<br>me : "
+ textField.getText());
changeStatus();
textField.setText("");
}
}
});
JLabel info = new JLabel("Press Enter or click");
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
WelcomeUser w = new WelcomeUser();
connection.disconnect();
w.setVisible(true);
}
});
// ////////////////////////////////////////////
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration config = new ConnectionConfiguration(
"prc.p1.im", 5222, "prc.p1.im");
connection = new XMPPConnection(config);
try {
connection.connect();
} catch (XMPPException e) {
e.printStackTrace();
System.out.println("Not Connected. Error :" + e.getMessage());
}
try {
connection.login(username, password);
} catch (XMPPException e) {
flag = false;
textField.setVisible(false);
sendButton.setVisible(false);
info.setText("Invalid Login!");
welcomeLabel.setText("Invalid user!");
}
connection.getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(final Chat chat,
final boolean createdLocally) {
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, final Message message) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
String sender = message.getFrom();
if (username.compareTo("usery") == 0)
sender = "h";
else
sender = "t";
if (message != null
|| message.getBody().isEmpty() == false
|| message.getBody() != null
|| message.getBody().toString()
.compareTo("null") == 1
&& message.getError() == null)
board.setText(board.getText()
+ "<br> <font color=red>"
+ sender
+ " </font>:<font color=black> "
+ message.getBody() + "</font>");
URL url = getClass().getResource(
"resource/ultrakill_ultimate.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
});
}
@Override
public void processMessage(Chat arg0, Message arg1) {
message = arg1.getBody();
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
详细信息: 正常的消息包是这样的:
<message id="5KE6T-11" to="usery@xyz" from="userx@xyz/Smack" type="chat">
<body>hi people</body>
<thread>fqw5912</thread>
<active xmlns="http://jabber.org/protocol/chatstates"/>
</message>
聊天状态数据包是这样的:
<message id="IgIbv-13" to="userx@xyz" from="usery@xyz/Smack" type="chat">
<thread>fqw5913</thread>
<composing xmlns="http://jabber.org/protocol/chatstates"/>
</message>
截屏 :
任何意见?