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

Pav*_* S. 9 xmpp google-talk contacts strophe

我使用strophe.js库在浏览器中发送和接收XMPP消息.它工作正常,但仅限于我已经在我的联系人列表中的用户 - 名单.

我需要在我的名册中添加一个人(我知道他的地址).我如何使用strophe.js实现这一目标?这对我很重要,因为gmail拒绝向我名单中没有的人发送消息.我想获得订阅:两者都能够接收和发送消息.

Mat*_*ttJ 12

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

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

当您的朋友接受时,他们也应该发送订阅给您,您可以通过为类型为"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");
Run Code Online (Sandbox Code Playgroud)