Java 忽略聊天中的ChatState数据包,并在SMACK的控制台中记录ChatState
我开发了一个简单的聊天客户端,它在两个用户(userx和usery)之间发送和接收消息以及用户键入CHatState notification。在调试窗口中,我看到包含body或chatstate的数据包。所有数据包都已正确记录 问题1:但我的窗口在聊天室(JLabel)中同时显示这两个窗口。因此,每当用户开始键入时,另一个用户在其窗口中看到一条空消息,这实际上是一个包含聊天状态通知的数据包,没有任何正文。我尝试过各种条件,但都失败了。我尝试了以下可笑的if条件,但它显示空数据包Java 忽略聊天中的ChatState数据包,并在SMACK的控制台中记录ChatState,java,smack,Java,Smack,我开发了一个简单的聊天客户端,它在两个用户(userx和usery)之间发送和接收消息以及用户键入CHatState notification。在调试窗口中,我看到包含body或chatstate的数据包。所有数据包都已正确记录 问题1:但我的窗口在聊天室(JLabel)中同时显示这两个窗口。因此,每当用户开始键入时,另一个用户在其窗口中看到一条空消息,这实际上是一个包含聊天状态通知的数据包,没有任何正文。我尝试过各种条件,但都失败了。我尝试了以下可笑的if条件,但它显示空数据包 if (mes
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();
}
}
}
import java.applet.applet;
公共类聊天板扩展JFrame实现MessageListener{
私有静态最终长serialVersionUID=1L;
私有JPanel内容窗格;
静态字符串用户名、密码;
静态XMPP连接;
私有JTextField textField;
静态JLabel板=新JLabel(“”);
静态聊天;
字符串消息;
静态状态;
静态字符串到;
静态布尔标志=false;
JLabel-typingStat;
公共类typingStatus实现了ChatStateListener{
@凌驾
公共无效状态已更改(聊天室arg0、聊天室arg1){
//TODO自动生成的方法存根
System.out.println(arg1.name());
}
@凌驾
public void processMessage(聊天arg0、消息arg1){
//TODO自动生成的方法存根
System.out.println(arg1.getBody());
}
}
公共void sendChat(字符串msg){
试一试{
至=“harsh00008”;
if(username.compareTo(to)==0)
to=”usery@xyz";
其他的
to=”userx@xyz";
chat=connection.getChatManager().createChat(to,this);
发送消息(msg);
}捕获(XMPPException e){
e、 printStackTrace();
}
}
公共状态(){
//TODO自动生成的方法存根
if(username.compareTo(“harsh00008”)==0)
chat=connection.getChatManager().createChat(“test@prc.p1.im",
这),;
其他的
chat=connection.getChatManager().createChat(
"harsh00008@prc.p1.im",本),;
if(textField.getText().isEmpty()==false&&flag==false){
试一试{
ChatStateManager.getInstance(连接).setCurrentState(
ChatState.composing,ChatBoard.chat);
flag=true;
}捕获(XMPPException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}否则{
if(textField.getText().isEmpty()==true&&flag==true){
试一试{
ChatStateManager.getInstance(连接).setCurrentState(
ChatState.paused,ChatBoard.chat);
flag=false;
}捕获(XMPPException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
}
}
公共聊天板(字符串用户、字符串通行证){
setVisible(真);
用户名=用户;
密码=通过;
//JFRAME创建代码省略
textField.getDocument().addDocumentListener(新DocumentListener()){
@凌驾
公共作废移除更新(文档事件e){
//TODO自动生成的方法存根
changeStatus();
}
@凌驾
公共作废插入更新(文档事件e){
//TODO自动生成的方法存根
changeStatus();
}
@凌驾
公共作废更改日期(记录事件e){
//TODO自动生成的方法存根
changeStatus();
}
});
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
if(textField.getText().isEmpty()==false){
sendChat(textField.getText().toString());
board.setText(board.getText()+“
me:”
+getText());
changeStatus();
textField.setText(“”);
}
}
});
JLabel info=新的JLabel(“按Enter键或单击”);
JButton exit=新JButton(“exit”);
exit.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
setVisible(假);
WelcomeUser w=新的WelcomeUser();
连接断开();
w、 setVisible(真);
}
});
// ////////////////////////////////////////////
XMPPConnection.DEBUG_ENABLED=true;
ConnectionConfiguration配置=新连接配置(
“prc.p1.im”,5222,“prc.p1.im”);
连接=新的XMPPConnection(配置);
试一试{
connection.connect();
}捕获(XMPPException e){
e、 printStackTrace();
System.out.println(“未连接。错误:+e.getMessage());
}
试一试{
连接。登录(用户名、密码);
}捕获(XMPPException e){
flag=false;
textField.setVisible(false);
sendButton.setVisible(false);
info.setText(“无效登录!”);
welcomeLabel.setText(“无效用户!”);
}
connection.getChatManager().addChatListener(新的ChatManagerListener()){
公共空间
<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>
if (message != null || message.getBody().isEmpty() == false
|| message.getBody() != null
|| message.getBody().toString().compareTo("null") == 1
&& message.getError() == null)
if (message.getBody() != null) // getBody() returns null if there is no body so check for empty is unnecessary.
if (<boolean condition> == false) // It is already evaluated to true or false
if (!<boolean condition>)
if (message.getBody().isEmpty() == false)
if (!message.getBody().isEmpty())
if (<boolean condition>)
/**
*
* listening for chat state change from server and change that state in
* model
*
* */
private class DefaultChatStateListener implements ChatStateListener {
public void stateChanged(Chat user, ChatState event) {
System.out.println("Getting called here...");
System.out.println(user.getParticipant() + " is " + event.name());
String state = event.name();
TypingStateType typingState = null;
if (state.equals("active")) {
typingState = TypingStateType.ACTIVE;
} else if (state.equals("composing")) {
typingState = TypingStateType.TYPING;
} else if (state.equals("paused")) {
typingState = TypingStateType.PAUSED;
} else if (state.equals("inactive")) {
typingState = TypingStateType.INACTIVE;
} else if (state.equals("gone")) {
typingState = TypingStateType.GONE;
}
controller.typingStateUpdated(genericConnection, typingState, user
.getParticipant().toString());
}
public void processMessage(Chat arg0, Message arg1) {
// Do nothing
}
}
lastChat = connection.getChatManager().createChat(
connection.getHost(),
new DefaultChatStateListener());
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class Test {
public static void main(String args[]) throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1", 5222);
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login("userx", "123456");
ChatManager cm = connection.getChatManager();
Chat chat = cm.createChat("usery", null);
/**
* add listener
*/
cm.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean create) {
chat.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat chat, Message msg) {
System.out.println(chat.getParticipant() + ":" + msg.getBody());
}
});
}
});
chat.sendMessage("test");
while(true); //keep this chat
//connection.disconnect();
}
}