使用 strophe.js 进行授权请求(添加到花名册)

10
我使用strophe.js库在浏览器中发送和接收XMPP消息,它能够正常工作,但仅适用于我已经在通讯录中的用户。
我需要将某个人(我已知其地址)添加到我的通讯录中,我该如何使用strophe.js实现呢?这对我很重要,因为Gmail拒绝向我未在通讯录中的人发送消息。我想获得订阅:双方均可接收和发送消息。
1个回答

12

发送 <presence to="friend@example.com" type="subscribe"/>:

conn.send($pres({ to: "friend@example.com", type: "subscribe" }));

当你的朋友接受请求时,他们也应该向你发送一个订阅,你可以通过设置一个类型为“subscribe”的Strophe处理程序来处理它们发送的出席信息:

function on_subscription_request(stanza)
{
    if(stanza.getAttribute("type") == "subscribe" && is_friend(stanza.getAttribute("from")))
    {
        // Send a 'subscribed' notification back to accept the incoming
        // subscription request
        conn.send($pres({ to: "friend@example.com", type: "subscribed" }));
    }
    return true;
}
conn.addHandler(on_subscription_request, null, "presence", "subscribe");

@MattJ 和 whit,你们能用 is_friend 方法更新一下代码吗? - pregmatch
1
@pregmatch 这取决于您的应用程序。它接收一个 JID,如果用户想接受他们的请求,则返回 true,否则返回 false。 - MattJ
@MattJ,我了解它的作用。但您是否已经与该名单进行了核对?我的问题更像是is_friend(jid)函数的外观如何。 - pregmatch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接