接收 Smack 的第一条消息时出现问题

发布于 2024-10-01 00:23:43 字数 1911 浏览 7 评论 0原文

我使用下面的代码发送消息。

 // 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生殊途 2024-10-08 00:23:43

当我在测试程序期间停止使用同一用户登录的 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.

可爱咩 2024-10-08 00:23:43

我已经有一段时间没有使用 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.

时光无声 2024-10-08 00:23:43

只是有点困惑。你说

上面的代码是简单的示例,但我的代码完全相同,只是我没有内联创建 PacketListener 实现。

如果没有 PacketListener 实现,如何接收消息?我认为由于下面的代码,您总是会收到来自您开始的聊天的消息

Chat newChat = chatmanager.createChat("[电子邮件受保护]" , 新的消息监听器() {
公共无效processMessage(聊天聊天,消息消息){
System.out.println("收到消息:" + message);
}
});

但为了异步等待传入消息,我认为您需要一个 PacketListener。
我可能完全误解了你面临的问题

(这应该是一条评论,但我不知道如何添加一个)

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("[email protected]", 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)

你げ笑在眉眼 2024-10-08 00:23:43

已经有一段时间了,你解决这个问题了吗?为什么要创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文