2012-02-29 229 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