如何在Strophe名册中的联系人之间发送和接收状态

时间:2019-05-04 14:13:50

标签: angular strophe

我希望花名册中的所有联系人列表都显示为在线(如果已连接)和离线(如果未连接)。

我做了什么?
我从xmpp服务器请求了用户名册,并创建了IQ节以请求用户节,然后将IQ节发送到服务器,因此我收到了请求的名册,然后从响应的query元素中提取了所有名册项并将其存储在userRosterClone < / p>

const userRosterClone = this.userRoster.slice(); 

我现在要做的是在名单中显示每个用户的在线状态,因此我创建了一个函数并发送了名单

this.requestUserPresence(userRosterClone);

requestUserPresence(contactList): void{
 for (let i = 0 ; i<contactList.length; i++){

   console.log("Requesting the user\'s presence...");

   // 1. Creating presence stanza to request the user's contact list stanza
   const presence = $pres({
     id: generateUUIDv4(),
     to: contactList[i].contactJid,
     type: "subscribe"

   })

   console.log("presence to request the user's presence:", presence.tree());

   // 2. Sending the presence stanza to the server
   this.connection.sendPresence(presence,
     this.onReceivingRequestedPresence.bind(this), // success callback
     // Note: binding to "this" will ensure "this" will refer to this component when onReceivingRequestedPresence is invoked
     // rather than have it refer to the Strophe.Connection object.
     (error) => console.log("Requesting the user's presence failed: ", error) // error callback
   );
    }
   }

问题出在onReceivingRequestedPresence无法正常工作,我无法将在线状态发送到已订阅类型的花名册。

 onReceivingRequestedPresence(presence){

console.log("Server Response to roster presence request: ", presence);

const pres= $pres({
  from: presence.to,
  id: presence.id,
  to: this.connection.jid,
  type: "subscribed"

})

 // 2. Sending the IQ stanza to the server
 this.connection.sendPresence(pres,
   // success callback
  // Note: binding to "this" will ensure "this" will refer to this component when onReceivingRequestedRoster is invoked
  // rather than have it refer to the Strophe.Connection object.
  (error) => console.log("Responsing the user's presence failed: ", error) // error callback
);

}

0 个答案:

没有答案