Search code examples
javaxmppsmack

Problem in receiving first Message by Smack


I use below code to send messages.

 // Assume we've created an XMPPConnection name "connection".
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("[email protected]", new MessageListener() {
    public void processMessage(Chat chat, Message message) {
        System.out.println("Received message: " + message);
    }
});

try {
    newChat.sendMessage("Howdy!");
}
catch (XMPPException e) {
    System.out.println("Error Delivering block");
}

And below for receiving messages sent to my JabberID, asynchronously.

// Create a packet filter to listen for new messages from a particular
// user. We use an AndFilter to combine two other filters.
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), 
        new FromContainsFilter("[email protected]"));
// Assume we've created an XMPPConnection name "connection".

// First, register a packet collector using the filter we created.
PacketCollector myCollector = connection.createPacketCollector(filter);
// Normally, you'd do something with the collector, like wait for new packets.

// Next, create a packet listener. We use an anonymous inner class for brevity.
PacketListener myListener = new PacketListener() {
        public void processPacket(Packet packet) {
            // Do something with the incoming packet here.
        }
    };
// Register the listener.
connection.addPacketListener(myListener, filter);

Sending message is ok.

But receiving message from another JabberID don't achived until I send a message to that JabberID.

And after that I receive messages sent by it properly.

Note that I often need to receive messages from jabberIDs that are not in my list and often My application is not the side that begins a chat.

Upper codes are smack samples but my code is completely same except I don't create PacketListener implementation inline.


Solution

  • My problem solved when I stopped using Jabber Client with the same user logined during I test my program. In other words code is correct but Jabber client catches sent messages and remain no things for my program to catch.