XMPP接收发送到不同资源的消息

发布于 2024-10-18 06:46:55 字数 1374 浏览 0 评论 0原文

如何接收发送到不同资源的 XMPP 聊天消息?

例如,我的消息侦听器可以正常接收消息,直到该消息从其他资源(例如 Gmail Google Talk)。从那时起,消息将发送到该客户端资源,而不是我的侦听器。

我正在使用 Java 的 Smack 库(实际上 asmack 这是 Android 的端口)

连接到服务(Google Talk 服务器)后,我添加一个如下监听器

    connection.addPacketListener(new PacketListener() {

        public void processPacket(Packet packet) {
            Log.i(TAG, "processPacket: chat");

            Message message = (Message) packet;

            Log.d(TAG, "Message: " + message.toXML());

            if (message.getBody() != null) {
                String fromName = StringUtils.parseBareAddress(message.getFrom());
                Log.i(TAG, "Got text [" +
                           message.getBody() +
                           "] from [" +
                           fromName +
                           "]");
            }
        }
    }, filter);

:最初工作正常,我收到来自 Google Talk 的消息。这些消息实际上会发送到多个客户端、我的桌面 Google Talk、Android 的 Google Talk 应用程序以及我的实现。

但是,当我回复消息(例如在桌面应用程序中)时,所有后续消息都会发送到桌面应用程序资源,并且在实现中我没有收到任何消息。

所以我不确定如何接收这些消息。 Google Talk 应用程序似乎就是这样做的。它不像最初那样显示为新消息(在回复之前),但它确实在 Google Talk 应用程序线程中进行了更新。

任何帮助都会很棒!

How do I receive XMPP chat messages that are sent to a different resource?

E.g. my message listener receives messages fine, until that message gets replied to from another resource (like Gmail Google Talk). From that point on the messages are sent to that client resource and not my listener.

I'm using the Smack library for Java (well, actually asmack which is a port for Android)

After connecting to the service (Google Talk server), I add a listener like this:

    connection.addPacketListener(new PacketListener() {

        public void processPacket(Packet packet) {
            Log.i(TAG, "processPacket: chat");

            Message message = (Message) packet;

            Log.d(TAG, "Message: " + message.toXML());

            if (message.getBody() != null) {
                String fromName = StringUtils.parseBareAddress(message.getFrom());
                Log.i(TAG, "Got text [" +
                           message.getBody() +
                           "] from [" +
                           fromName +
                           "]");
            }
        }
    }, filter);

This works fine initially, I receive messages from Google Talk. These messages actually get sent to multiple clients, my desktop Google Talk, the Google Talk app an Android, and my implementation.

But when I reply to a message, say in the desktop application, all subsequent messages get sent to the desktop application resource, and I get nothing received in my implementation.

So I'm not sure how to also receive these messages. The Google Talk application seems to do this. It doesn't appear as a new message like it does initially (before it's replied to), but it does get updated in the Google Talk application thread.

Any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

若言繁花未落 2024-10-25 06:46:55

如果我没理解错的话,您将在不同的应用程序(资源)上使用同一用户登录。您收到来自名册中联系人的消息,但是当您从一个地方回复时,它会在这两个节点之间创建对话吗?

这是特定于实现的。大多数客户端遵循 RFC 3921 中推荐的行为,即回复完整的JID (user@domain/resource) 如果收到的消息来自完整的 JID。您可以自由地使用您的 from 作为您的裸 JID (user@domain) 进行回复,以便将来的响应将按顺序发送到您的裸 JID,从而获得您所有可用的资源(假设优先级是相等)接收它。

从您从桌面应用程序回复的示例来看,该应用程序可能会将其“发件人”标头设置为完整的 JID,而从您的桌面应用程序接收回复的另一方则使用该值作为“收件人”其响应的标头。这当然意味着只有唯一的资源才会收到消息。

所以这和你的代码无关。它与桌面应用程序中的代码有关,与另一个端点(与您交谈的伙伴)的代码结合使用,从而将您的实现从循环中剔除。

If I've understood you right, you are logged in with the same user on different applications (resources). You receive messages from contacts in your roster, but when you reply from one place, it creates a dialogue just between those two nodes?

This is implementation specific. Most clients follow the recommended behaviour in RFC 3921, which is to reply to a full JID (user@domain/resource) if the message received was from a full JID. You are free to reply using your from as your bare JID (user@domain), so that a future response in sequence will be sent to your bare JID, resulting in all your available resources (assuming priority is equal) receiving it.

From your example where you reply from your desktop application, it's likely that the application is setting its 'From' header to the full JID, and the other party whom receives the reply from your desktop application is then using that value as the 'To' header for its response. This of course means only that unique resource will get the messages.

So it's got nothing to do with your code. It's to do with the code in that desktop application, working in conjunction with the code at the other endpoint (the buddy you're talking to), that is cutting your implementation out of the loop.

不必你懂 2024-10-25 06:46:55

我认为你应该在 xmpp 中查找该问题的优先级设置。需要设置一个优先级设置,以便跨不同资源接收消息!

对于多个连接,您需要控制消息流,因此需要设置优先级: 以下是优先级的基本规则: 在任何给定时间具有最高优先级的资源将是接收传入消息的资源。如果两个或多个资源具有相同的优先级,则具有所述优先级的所有资源都将接收传入消息。如果所有连接的资源都具有负优先级,则传入消息将在服务器端排队,直到其中一个资源将优先级重置为正。

来源:http://blog.roobix.net/2010 /02/jabber-xmpp-resources-and-priorities.html

I think you should look up priority settings for that problem in xmpp. there is a priority settings that needs to be set so as to receive messages across different resources !

with multiple connection you need to control the message flow and so need to set priorities: Here are the basic rules for priorities: The resource with the highest priority at any given time will be the one which receives incoming messages. If two or more resources have the same priority, all resources with said priority will receive incoming messages. If all connected resources have a negative priority, incoming messages will be queued server-side until one of the resources resets priority to be positive.

source:http://blog.roobix.net/2010/02/jabber-xmpp-resources-and-priorities.html

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