java 如何添加订阅模式“两者”的名册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5802607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:50:29  来源:igfitidea点击:

how to add roster with subscription mode "both"

javaxmppsmack

提问by snowway

i'm using smack 3.1.0, and when i add a roster,i can't get subscription "both". who can help me? below is my code:

我正在使用 smack 3.1.0,当我添加花名册时,我无法获得“两者”订阅。谁能帮我?下面是我的代码:

Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
Roster roster = connection.getRoster();
roster.createEntry("[email protected]","me",null)

after the code execution, i observed in openfire the subscription is "to"

代码执行后,我在 openfire 中观察到订阅是“to”

回答by Joe Hildebrand

Rewriting @mschonaker's answer to be a little more clear.

重写@mschonaker 的答案,使其更加清晰。

Both users need to subscribe to each other andaccept the subscription request they received. Let's call them Alice and Bob. Alice sends a subscription request to Bob:

两个用户都需要互相订阅接受他们收到的订阅请求。让我们称他们为 Alice 和 Bob。Alice 向 Bob 发送订阅请求:

Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('[email protected]');
connection.sendPacket(subscribe);

When Bob receives the request, he approves it:

当 Bob 收到请求时,他会批准它:

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo('[email protected]');
connection.sendPacket(subscribed);

Bob may also be interested in Alice's presence, so he subscribes to her:

Bob 可能也对 Alice 的存在感兴趣,所以他订阅了她:

Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo('[email protected]');
connection.sendPacket(subscribe);

And Alice needs to approve Bob's request:

并且 Alice 需要批准 Bob 的请求:

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo('[email protected]');
connection.sendPacket(subscribed);

Section 3.1 of RFC6121is the current best reference for how this works.

RFC6121 的第 3.1 节是当前有关其工作原理的最佳参考。

回答by mschonaker

Both users need to subscribe to each other. By sending a presence subscription stanza. In Smack:

两个用户都需要互相订阅。通过发送出席信息订阅节。在 Smack 中:

    Presence presence = new Presence(Presence.Type.subscribe);
    presence.setTo(jid);
    connection.sendPacket(presence);

Section 3.1 of the RFC6121will give you the semantic details.

RFC6121 的第 3.1 节将为您提供语义细节。

回答by Bam

Okay, I toiled hard at this for a couple of days and finally got things working. Thank you @Joe Hildebrand, your answer put me on the right track to solve this. I have implemented it with a manual subscription mode (ie. user needs to accept another user's request manually).

好吧,我为此苦苦挣扎了几天,终于让事情开始了。谢谢@Joe Hildebrand,你的回答让我走上了解决这个问题的正确轨道。我已经使用手动订阅模式实现了它(即用户需要手动接受另一个用户的请求)。

The server keeps pushing subscribe request to the user (upon re-login) if the user hasn't sent a subscribed or unsubscribed back. So what you can do is save the incoming subscribe requests locally in a list and display that as a "friend request list" for manual accept/reject. If your application gets restarted (and hence re-connects to server), the server will push subscribe requests again.

如果用户没有发送订阅或取消订阅,服务器会不断向用户推送订阅请求(重新登录时)。因此,您可以做的是将传入的订阅请求保存在本地列表中,并将其显示为手动接受/拒绝的“好友请求列表”。如果您的应用程序重新启动(并因此重新连接到服务器),服务器将再次推送订阅请求。

This is how it works:

这是它的工作原理:

  • User1 sends subscribe presence to User2.
  • Roster entry gets automatically created in User1's roster (but not in User2's roster).
  • User2 receives subscribe request from User1.
  • User2 sends back a subscribed presence to User2 (User2 > User1 subscription complete).
  • User2 checks if User1 is in User2's roster. User1 is not in User2's roster. User2 sends back a subscribe presence to User1.
  • Roster entry gets automatically created in User2's roster.
  • User1 receives subscribe presence from User2.
  • User1 checks if User2 is in User1's roster. User2 is in User1's roster. User1 sends back a subscribed presence to User2 (User2 > User1 subscription complete).

            final Presence newPresence = (Presence) packet;
            final Presence.Type presenceType = newPresence.getType();
            final String fromId = newPresence.getFrom();
            final RosterEntry newEntry = getRoster().getEntry(fromId);
    
            if (presenceType == Presence.Type.subscribe)
            {
                //from new user
                if (newEntry == null)
                {
                    //save request locally for later accept/reject
                    //later accept will send back a subscribe & subscribed presence to user with fromId
                    //or accept immediately by sending back subscribe and unsubscribed right now
                }
                //from a user that previously accepted your request
                else
                {
                    //send back subscribed presence to user with fromId
                }
            }
    
  • User1 向 User2 发送订阅状态。
  • 名册条目在 User1 的名册中自动创建(但不在 User2 的名册中)。
  • 用户 2 收到用户 1 的订阅请求。
  • 用户 2 将订阅的状态发送回用户 2(用户 2 > 用户 1 订阅完成)。
  • User2 检查 User1 是否在 User2 的花名册中。User1 不在 User2 的花名册中。用户 2 将订阅状态发送回用户 1。
  • 名册条目会在 User2 的名册中自动创建。
  • User1 从 User2 接收订阅状态。
  • User1 检查 User2 是否在 User1 的花名册中。User2 在 User1 的花名册中。用户 1 将订阅的状态发送回用户 2(用户 2 > 用户 1 订阅完成)。

            final Presence newPresence = (Presence) packet;
            final Presence.Type presenceType = newPresence.getType();
            final String fromId = newPresence.getFrom();
            final RosterEntry newEntry = getRoster().getEntry(fromId);
    
            if (presenceType == Presence.Type.subscribe)
            {
                //from new user
                if (newEntry == null)
                {
                    //save request locally for later accept/reject
                    //later accept will send back a subscribe & subscribed presence to user with fromId
                    //or accept immediately by sending back subscribe and unsubscribed right now
                }
                //from a user that previously accepted your request
                else
                {
                    //send back subscribed presence to user with fromId
                }
            }
    

回答by Ram

If you are using open fire server you can also use User Service pluginwhich will create roster with subscription both...

如果您使用的是开放式火服务器,您还可以使用 用户服务插件,该插件将创建具有订阅功能的花名册...

回答by Vinod Makode

Same problem I was face but I got the solution how subscribe set 'both'

我遇到了同样的问题,但我得到了如何订阅“两者”的解决方案

Below is sending subscription to user, when you added the user

下面是向用户发送订阅,当您添加用户时

 Presence pres = new Presence(Presence.Type.subscribed);
        pres.setPriority(24);
        pres.setMode(Presence.Mode.available);
        pres.setTo(friendJid);

        RoosterConnection.getConnection().sendStanza(pres);

and Receiving end put below method in connection class and presenceChanged is default method of RosterListener.

和 Receiving end 放在连接类中的方法下面,presenceChanged 是 RosterListener 的默认方法。

 @Override
public void presenceChanged(Presence presence) {
    mBus.post(presence);
    try {
        Presence pres = new Presence(Presence.Type.subscribed);
        pres.setTo(presence.getFrom());
        RoosterConnection.getConnection().sendStanza(pres);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}