How to know Typing Status in XMPP openfire using S

2019-02-21 19:00发布

I am developing chat application by using Openfire XMPP server. I can text chat between two user. But i want to know Typing status when some one is typing message. So i created a class :-

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());

    }

}

But i am confuse so that How will it work? I know that i need a packet where i can it in Listener. But i am unable to find that packet.

Please any one suggest, How will it work?

and also what is difference between Smack and asmack?

Thank you!

8条回答
做自己的国王
2楼-- · 2019-02-21 19:27

However you can get it from ProcessPacket also. there you will get a Message object, after you can extract xml portion from there and handle them its contain specific chatstate or not.

    Message message = (Message) packet;
    String msg_xml = message.toXML().toString();

    if (msg_xml.contains(ChatState.composing.toString())) {
        //handle is-typing, probably some indication on screen
    } else if (msg_xml.contains(ChatState.paused.toString())) {
        // handle "stopped typing"
    } else {
       // normal msg
    }

now handle as per your requirement.

查看更多
Juvenile、少年°
3楼-- · 2019-02-21 19:35

Just add ChatStateManager after ChatManager intalization:

chatManager =  ChatManager.getInstanceFor(getXmpptcpConnection());
ChatStateManager.getInstance(getXmpptcpConnection());

Then you need to add ChatStateListener during createChat(to,chatMesageListener):

chatManager.createChat(message.getTo(), chatMessageListener).sendMessage(message);

private ChatStateListener chatMessageListener = new ChatStateListener() {

    @Override
    public void stateChanged(Chat chat, ChatState state) {
        //State Change composing,active,paused,gone,etc
        Log.d(TAG, "ChatStateListener:::stateChanged -> " + chat.toString() + " \n -> " + state.toString());
    }

    @Override
    public void processMessage(Chat chat, Message message) {
        //Incoming Message
        Log.d(TAG, "ChatStateListener:::processMessage -> " + chat.toString() + " \n -> " + message.toString());
    }
};
查看更多
登录 后发表回答