使用Asmack在XMPP中回显消息

时间:2014-02-21 07:47:23

标签: android message asmack

asmack documentation可以看到,当你从Chat的MessageListener收到processMessage()方法中的一个聊天类型消息时,我应该使用Chat.sendMessage()发回这条消息来通知消息接收。 / p>

但是在我的聊天实现中,我在打开一对一聊天时不使用ChatManager,我有一个服务和监听器来做这些事情。

在服务中我有一个PacketListener来接收,我正在尝试正确地发送回发消息,正如我在asmack documentation中看到的那样,但它不起作用。

this.xmppChat.addPacketListener(new PacketListener() {

            @Override
            public synchronized void processPacket(Packet packet) {

                Message message = (Message) packet;

                if (message.getType() == Message.Type.chat) {

                    Log.i("XMPP_SERVICE",
                            String.format("Mensaje recibido de %s",
                                    message.getFrom()));

                    // Rest of code implementation...

                    // At the end try to send back message to notify reception.
                    XMPPService.this.xmppChat.sendMessage(new Message(message.getBody()));
                }
            }
        }, new PacketTypeFilter(Message.class));

XMPPService.this.xmppChat.sendMessage(消息消息)方法:

public boolean sendMessage(Message message) {

    Log.i("XMPP_INFO", "Enviando el mensaje");

    try {

        this.xmppManager.getConnection().sendPacket(message);

        Log.i("XMPP_INFO", "Enviado el mensaje");
        return true;
    } catch (Exception ex) {

        Log.e("XMPP_ERROR", ex.getMessage());
        return false;
    }
}

我做错了什么?,我该如何解决?

致以最诚挚的问候,并提前感谢您。

2 个答案:

答案 0 :(得分:2)

这是我的代码,希望对您有所帮助,请看:

ChatManager cm = XmppTool.getConnection().getChatManager();


final Chat newchat = cm.createChat(userChat, new MessageListener() {

            @Override
            public void processMessage(Chat chat, Message message) {
                // TODO Auto-generated method stub
                Log.i(TAG,
                        "from:" + message.getFrom() + " body:"
                                + message.getBody());
                String[] args = new String[] { "hoafer", message.getBody(),
                        TimeRender.getDate(), "IN" };
            }
        });

如果您想收听所有留言,可以使用该代码:

cm.addChatListener(new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean able) {
            chat.addMessageListener(new MessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Log.i(TAG, "receiver message~!");
                    String tmp = message.getFrom();
                    String body=message.getBody();
                    String[] from = tmp.split("@");
                    Log.i(TAG, "from:" + message.getFrom() + " body:"
                            + body);
                    }

            });
        }
    });

建议您使用最新版本的库,旧版本似乎有问题。我遇到了类似的问题,所以使用新版本库解决了它

答案 1 :(得分:0)

试试这段代码可以帮到你:

PacketFilter packetFilter = new MessageTypeFilter(
                        Message.Type.chat);

                connection.addPacketListener(new PacketListener() {
                    @Override
                    public void processPacket(Packet packet)
                            throws NotConnectedException {
                        Message message = (Message) packet;

                        if (message.getBody() != null) {
                            String fromName = StringUtils
                                    .parseBareAddress(message.getFrom());
                            Log.e("XMPPChatDemoActivity", "Text Recieved "
                                    + message.getBody() + " from "
                                    + fromName);

                            Packet received = new Message();
                            received.addExtension(new DeliveryReceipt(
                                    packet.getPacketID()));
                            received.setTo(packet.getFrom());
                            connection.sendPacket(received);


                        }

                    }
                }, packetFilter);