接收 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);
发送消息就OK了。
但是,直到我向该 JabberID 发送消息后,才能从另一个 JabberID 接收消息。
之后我正确地收到它发送的消息。
请注意,我经常需要从不在我的列表中的 jabberID 接收消息,并且我的应用程序通常不是开始聊天的一方。
上面的代码是简单的示例,但我的代码完全相同,只是我没有内联创建 PacketListener 实现。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我在测试程序期间停止使用同一用户登录的 Jabber 客户端时,我的问题得到了解决。
换句话说,代码是正确的,但 Jabber 客户端捕获发送的消息,并且我的程序没有捕获任何内容。
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.
我已经有一段时间没有使用 smack 了,但我设法根据传入的消息开始聊天。
如果我没记错的话,我有某种“ChatRegistry”,一个简单的
Map
,其中键等于聊天伙伴 id。然后我监听传入的消息,提取 jabber id 并查找与该伙伴的活动聊天会话。如果没有活动会话,我会创建一个新的聊天并将新的键/值对添加到注册表中。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.只是有点困惑。你说
如果没有 PacketListener 实现,如何接收消息?我认为由于下面的代码,您总是会收到来自您开始的聊天的消息
但为了异步等待传入消息,我认为您需要一个 PacketListener。
我可能完全误解了你面临的问题
(这应该是一条评论,但我不知道如何添加一个)
Just a bit confused. You say
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
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)
已经有一段时间了,你解决这个问题了吗?为什么要创建 FromContainsFilter?这样,您的侦听器仅处理来自给定用户的数据包,而不是所有数据包。
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.