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("jsmith@jivesoftware.com", 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, asynchronous开发者_运维问答ly.
// 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("mary@jivesoftware.com"));
// 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.
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.
It's been quite a while since I worked with smack, but I managed to start chats based on incoming messages.
If I remember well, I had some sort of "ChatRegistry", a simple Map<String, Chat>
where the key was equal to the chat partners id. Then I listened to incoming messages, extracted the jabber id and looked up the active chat session with this partner. If there wasn't an active session, I created a new Chat and added the new key/vale pair to the registry.
Just a bit confused. You say
Upper codes are smack samples but my code is completely same except I don't create PacketListener implementation inline.
How do you receive messages without having a PacketListener implementation? I would think that you would always receive messages from chats you started because of the code below
Chat newChat = chatmanager.createChat("jsmith@jivesoftware.com", new MessageListener() { public void processMessage(Chat chat, Message message) { System.out.println("Received message: " + message); } });
But in order to asynchronously wait for incoming messages, I would think you will need a PacketListener. I might have totally misunderstood the problem you are facing though
(This should have been a comment, but I can't figure how to add one)
It's been a while, did you manage to solve this? why are you creating FromContainsFilter? In that way, your listeners processes only packets from the given user, not all packets.
精彩评论