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

Receiving multiple messages after sending in group chat using 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);
    }

}

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

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

其中DiscussionHistoryclass控制字符数 或进入房间时收到的消息。房间决定 如果您不指定一个,则历史数量为 return DiscussionHistory 加入房间时。了解更多信息 refer this

示例:

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