我希望花名册中的所有联系人列表都显示为在线(如果已连接)和离线(如果未连接)。
我做了什么? 我从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.addHandler(this.
onReceivingRequestedPresence.bind(this), null, "presence");
this.connection.sendPresence(presence);
}
问题出在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)
}