使用asmack在群聊中发送后接收多条消息

时间:2016-05-11 13:43:04

标签: android xmpp asmack

发送消息后,它正在接收到该组。但与此同时,我又收到了以前的所有消息。帮助我解决这个问题。 这是我的代码:

send.setOnClickListener(new View.OnClickListener() {            
    public void onClick(View view) {
        to = groupname + "@conference.localhost".toString();
        String text = (textMessage.getText().toString());
        textMessage.setText("");

        muc = new MultiUserChat(connection, to);

        try {
            muc.join(USERNAME);
        } catch (XMPPException e) {
            e.printStackTrace();
        }

        Message msg = new Message(to, Message.Type.groupchat);
        msg.setBody(text);
        if (connection != null) {
            connection.sendPacket(msg);
            Msg data = new Msg();
            data.setMessage(text);
            data.setName(userFrom);
            data.setDate(DateAndTime.getCurrentDate());
            data.setTime(DateAndTime.getCurrentTime());
            data.setSender(true);
            MessageListAdapter.messagesItems.add(data);
            notifyMyAdapter();
        }
    }
});

这是我的接收代码:

public void setConnection(XMPPConnection connection) {

    this.connection = connection;
    if(connection != null) {

        PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
        connection.addPacketListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {

                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                 Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);
                    Msg data = new Msg();
                    String subject = message.getSubject();

                    if (subject == null) {
                        data.setMessage(message.getBody());
                        data.setName(NAME);
                        data.setDate(DateAndTime.getCurrentDate());
                        data.setTime(DateAndTime.getCurrentTime());
                        data.setSender(false);
                        MessageListAdapter.messagesItems.add(data);

                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            notifyMyAdapter();
                        }
                    });
                } else if (subject.equals("")){
                    data.setMessage(message.getBody());
                    data.setName(NAME);
                    data.setDate(DateAndTime.getCurrentDate());
                    data.setTime(DateAndTime.getCurrentTime());
                    data.setSender(false);
                    MessageListAdapter.messagesItems.add(data);
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            notifyMyAdapter();
                        }
                    });
                }
            }
        }

        },filter);
    }

}

1 个答案:

答案 0 :(得分:0)

MultiUserChat中要控制加入会议室时要接收的历史记录数量,您需要提供已配置的DiscussionHistory对象。有关MultiUserChat refer this

的更多信息
muc.join(String nickname, String password, DiscussionHistory history, long timeout)

其中DiscussionHistory类控制字符数 或进入房间时收到的消息。房间将决定 如果您未指定a,则返回的历史记录数量 DiscussionHistory加入一个房间。了解更多信息refer this

示例:

MultiUserChat muc = new MultiUserChat(connection, to);
DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0);
muc.join("username", "password", history, SmackConfiguration.getPacketReplyTimeout());