我使用下面的代碼發送消息。收到Smack的第一條消息時出現問題
// 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");
}
下面用於接收發送到我的JabberID的消息,異步。
// 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);
發送消息是可以的。
但是,接收來自另一個JabberID的消息在我向該JabberID發送消息之前無法獲得。
然後我收到正確發送的消息。
請注意,我經常需要接收來自不在我列表中的jabberID的消息,而且我的應用程序經常不是開始聊天的一方。
上面的代碼是很好的示例,但我的代碼完全相同,除非我不創建PacketListener實現內聯。
雖然我在想你的問題,請仔細看看[用smack調試](http://www.igniterealtime.org/builds/smack/docs/latest/documentation/debugging.html)。在simlpy打開調試器後,我幾乎可以解決所有問題;-) – 2010-11-09 13:09:32
您的代碼是否完整?我沒有任何調用myCollector.nextPacket()... – mschonaker 2011-05-25 21:44:36