smack-在名册中创建条目

发布于 2024-11-19 20:09:52 字数 920 浏览 4 评论 0原文

我知道这个问题之前已经被问过,但 mschonaker 仅给出了部分答复 这里。在我的网站上,用户可以将人员添加到他们的名册中,然后好友必须接受,最后他们就建立了联系。第一个人(用户a)使用著名的

roster.createEntry(jid, name, groups);

,它可以工作并在他的名册中添加一个条目,但我对该怎么做有点困惑:

  • 我如何接收另一端的请求?我尝试实现 PacketListener,覆盖 processPacket() 并检查类型为 Presence.Type.subscribe 或 Presence.Type.subscribed 的数据包,但似乎它仅针对用户 a 触发,而不是应该侦听的数据包subscriptions- user b.

  • 然后,我有另一个功能可以在登录时查找所有请求,因此如果我再次登录,我将看到该请求,但我如何接受它?起初,我认为用户 b 也应该将用户 a 添加到他的名册中 roster.createEntry(jid, name, groups);

但这不起作用,什么也没有发生。我也尝试这样做

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(jid);
xMPPConnection.sendPacket(subscribed);

,但也没有成功。我确信一定有一种好的、简单的方法可以做到这一点,但到目前为止我还没有在任何地方找到它,而且一次尝试一件事让我太头疼了。有谁知道正确的流程吗?提前致谢!

I know that this question has been asked before, but only a partial response has been given by mschonaker here. On my website, user can add people to their roster, then the buddy has to accept and finally they are connected. The first person (user a) use the famous

roster.createEntry(jid, name, groups);

which works and add an entry in his roster, but then I am a bit confused to what to do:

  • how do I receive the request on the other end? I tried implementing a PacketListener, override processPacket() and check for packet which types are Presence.Type.subscribe or Presence.Type.subscribed, but it appears that it is only triggered for the user a, but not the one that should listen for subscriptions- user b.

  • then, I have another function that can lookup all requests at login, so if I login again I will see the request, but how do I accept it? at first, I thought that the user b should also add user a in his roster by
    roster.createEntry(jid, name, groups);

but that didn't work and nothing was happening. I also tried to do

Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(jid);
xMPPConnection.sendPacket(subscribed);

but didn't work either. I'm sure there must be a good and simple way to do it, but I haven't found it so far anywhere, and trying one thing at a time gave me too many headaches. Does anyone know the correct flow for this? thanks in advance!

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

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

发布评论

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

评论(1

来世叙缘 2024-11-26 20:09:52

来自 Smack 文档:
名册和状态使用基于权限的模型,用户必须先授予权限,然后才能将其添加到其他人的名册中。这样可以确保只有经过批准的用户才能查看其状态信息,从而保护用户的隐私。因此,当您添加新的名册条目时,它将处于待处理状态,直到其他用户接受您的请求。
如果其他用户请求在线状态订阅以便将您添加到他们的名册中,您必须接受或拒绝该请求。 Smack 通过以下三种方式之一处理状态订阅请求:

Automatically accept all presence subscription requests.
Automatically reject all presence subscription requests.
Process presence subscription requests manually. 

可以使用 Roster.setSubscriptionMode(Roster.SubscriptionMode) 方法设置模式。简单客户端通常使用其中一种自动订阅模式,而全功能客户端应手动处理订阅请求并让最终用户接受或拒绝每个请求。如果使用手动模式,则应注册一个 PacketListener,用于侦听类型为 Presence.Type.subscribe 的 Presence 数据包。

因此,尝试将Roster的订阅模式设置为手动,然后实现PacketListener来监听Presence.Type.subscribe。收到数据包后,使用 Presence.Type.subscribed 创建一个新数据包并将其发送给发送者。

From the Smack documentation:
Rosters and presence use a permissions-based model where users must give permission before they are added to someone else's roster. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry it will be in a pending state until the other user accepts your request.
If another user requests a presence subscription so they can add you to their roster, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:

Automatically accept all presence subscription requests.
Automatically reject all presence subscription requests.
Process presence subscription requests manually. 

The mode can be set using the Roster.setSubscriptionMode(Roster.SubscriptionMode) method. Simple clients normally use one of the automated subscription modes, while full-featured clients should manually process subscription requests and let the end-user accept or reject each request. If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of Presence.Type.subscribe.

So, try setting Roster's subscription mode to manual, and then implement PacketListener to listen for Presence.Type.subscribe. Once a packet is received, create a new Packet with Presence.Type.subscribed and send it to the sender.

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