2012-02-29 162 views
7

我用strophe.js库在浏览器中发送和接收XMPP消息(加入名册)。它工作正常,但仅适用于我的联系人列表中已有的用户 - 名单。授权请求,利用strophe.js

我需要有人(其地址,我知道)添加到我的名册。我如何使用strophe.js来实现这一点?这对我来说很重要,因为gmail拒绝向我名单中没有的人发送消息。我想要订阅:都可以接收和发送消息。

回答

10

发送<presence to="[email protected]" type="subscribe"/>

conn.send($pres({ to: "[email protected]", type: "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: "[email protected]", type: "subscribed" })); 
    } 
    return true; 
} 
conn.addHandler(on_subscription_request, null, "presence", "subscribe"); 
+0

谢谢,这正是我需要的。 – 2012-03-05 22:18:10

+0

@MattJ和白衣is_friend方法,你能用它更新的代码? – pregmatch 2013-11-21 13:15:38

+1

@pregmatch这取决于你的应用程序。它收到一个JID,如果用户想要接受他们的请求,应该为true,否则为false。 – MattJ 2013-11-21 23:18:08