How to know Typing Status in XMPP openfire using S

2019-01-07 08:38发布

问题:

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!

回答1:

To enable ChatStateListener you need to create a custom MessageListener Class

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

    }

}

Then you create MessageListener object

MessageListener messageListener = new MessageListenerImpl();

And then pass this in the create chat method

Chat newChat = chatmanager.createChat(jabber_id_of_friend, messageListener);

what is difference between Smack and asmack? <-- Check This



回答2:

finally I got the solution. I need to use chat listener along with chat manager and also I need to use in built sendcomposingnotification function. No need to use Messageeventrequestlistener interface or any other custom class for this. I added the following lines,,

connection.getChatManager().addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(final Chat arg0, final boolean arg1) {
                // TODO Auto-generated method stub

                arg0.addMessageListener(new MessageListener()
                {

                    @Override
                    public void processMessage(Chat arg0, Message arg1) 
                    {
                        // TODO Auto-generated method stub

                        Log.d("TYpe Stat",title[0] + " is typing......");
                        Toast.makeText(getApplicationContext(),title[0] + " is typing......",Toast.LENGTH_SHORT).show();
                    }



                    }
                }); 
            }
        });

and also need to send notification like this..

mem.sendComposingNotification(etRecipient.getText().toString(), message.getPacketID());
        System.out.println("Sending notification");

where mem is type of MessageEventManger. Ref: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html



回答3:

ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat= chatManager.createChat(to, new ChatStateListener() {
      @Override
      public void stateChanged(Chat chat, ChatState state) {
                switch (state){
                        case active:
                             Log.d("state","active");
                             break;
                        case composing:
                             Log.d("state","composing");
                             break;
                        case paused:
                             Log.d("state","paused");
                             break;
                        case inactive:
                             Log.d("state","inactive");
                             break;
                        case gone:
                             Log.d("state","gone");
                             break;
                 }
        }

        @Override
        public void processMessage(Chat chat, Message message) {
               Log.d("processMessage","processMessage");
        }
});

use this code.hope so will work



回答4:

i am using chat state listener :

Chat chat = chatManager.createChat(jid,
                        new ChatStateChangedListener());

bind the chatstatelistener with each jid like above , then :

public class ChatStateChangedListener implements ChatStateListener {

    public ChatStateChangedListener() {
        printLog("Chat State Changed Listner Constructor");
    }

    @Override
    public void processMessage(Chat arg0, Message arg1) {

    }


    @Override
    public void stateChanged(Chat chat, ChatState state) {

        if (state.toString().equals(ChatState.composing.toString())) {
                tvLastSeen.setText("Typing...");
            } else if (state.toString().equals(ChatState.paused.toString())) {
                tvLastSeen.setText("paused...");
            } else {
                tvLastSeen.setText("nothing");
            }

        }


    }

}


回答5:

Create On Class MMessageListener to listen incoming messages

private class MMessageListener implements MessageListener, ChatStateListener {

    public MMessageListener(Context contxt) {
    }


    @Override
    public void stateChanged(Chat chat, ChatState chatState) {

        mStatus = "Online";

        if (ChatState.composing.equals(chatState)) {

            mStatus = chat.getParticipant() + " is typing..";
            Log.d("Chat State", chat.getParticipant() + " is typing..");
        } else if (ChatState.gone.equals(chatState)) {
            Log.d("Chat State", chat.getParticipant() + " has left the conversation.");
            mStatus = chat.getParticipant() + " has left the conversation.";
        } else if (ChatState.paused.equals(chatState)){
            Log.d("Chat State", chat.getParticipant() + ": " + chatState.name());
            mStatus = "Paused";
        }else if (ChatState.active.equals(chatState)){
            mStatus = "Online";
        }

        // do whatever you want to do once you receive status


    }


    @Override
    public void processMessage(Message message) {

    }

    @Override
    public void processMessage(Chat chat, Message message) {


    }
}

Add Listener to your chat object

Chat Mychat = ChatManager.getInstanceFor(connection).createChat( "user2@localhost"), mMessageListener);

Send status to receiving user on edittext text change

ChatStateManager.getInstance(connection).setCurrentState(ChatState.composing, Mychat);

This works fine for me.



回答6:

Your or another xmpp client which you use, should sending chat state for You can catch the state.

Like This;

try {
      ChatStateManager.getInstance(GlobalVariables.xmppManager.connection).setCurrentState(ChatState.composing, chat);
    } catch (XMPPException e) {
        e.printStackTrace();
    }

or

try {
        ChatStateManager.getInstance(GlobalVariables.xmppManager.connection).setCurrentState(ChatState.gone, chat);
    } catch (XMPPException e) {
        e.printStackTrace();
    }


回答7:

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.



回答8:

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