我正在使用Openfire XMPP服务器开发聊天应用程序.我可以在两个用户之间进行文字聊天但我想知道某人输入消息时的打字状态.所以我创建了一个类: –
public class typingStatus implements ChatStateListener { @Override public void processMessage(Chat arg0,Message arg1) { // TODO Auto-generated method stub } @Override public void stateChanged(Chat arg0,ChatState arg1) { // TODO Auto-generated method stub System.out.println(arg0.getParticipant() + " is " + arg1.name()); } }
但我很困惑,所以它将如何运作?我知道我需要一个数据包,我可以在Listener中使用它.但我无法找到该数据包.
请任何人建议,它将如何运作?
还有Smack和asmack有什么区别?
谢谢!
解决方法
要启用ChatStateListener,您需要创建自定义MessageListener类
public class MessageListenerImpl implements MessageListener,ChatStateListener { @Override public void processMessage(Chat arg0,Message arg1) { System.out.println("Received message: " + arg1); } @Override public void stateChanged(Chat arg0,ChatState arg1) { if (ChatState.composing.equals(arg1)) { Log.d("Chat State",arg0.getParticipant() + " is typing.."); } else if (ChatState.gone.equals(arg1)) { Log.d("Chat State",arg0.getParticipant() + " has left the conversation."); } else { Log.d("Chat State",arg0.getParticipant() + ": " + arg1.name()); } } }
然后创建MessageListener对象
MessageListener messageListener = new MessageListenerImpl();
然后在create chat方法中传递它
Chat newChat = chatmanager.createChat(jabber_id_of_friend,messageListener);
Smack和asmack有什么区别? < - Check This