是否存在一种基于Strophe.js的聊天应用程序的在线状态处理现有解决方案?
我有一个简单的基于Strophe.js的聊天应用程序。我想仅显示在线用户并动态更改列表。我想知道是否存在任何现有解决方案(可能是Strophe插件)来处理这个问题。如果不存在这样的解决方案,那么实施它的最佳/最简单方法是什么?
是否存在一种基于Strophe.js的聊天应用程序的在线状态处理现有解决方案?
我有一个简单的基于Strophe.js的聊天应用程序。我想仅显示在线用户并动态更改列表。我想知道是否存在任何现有解决方案(可能是Strophe插件)来处理这个问题。如果不存在这样的解决方案,那么实施它的最佳/最简单方法是什么?
iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
App.connection.sendIQ(iq, your_roster_callback_function);
your_roster_callback_function(iq){
$(iq).find('item').each(function(){
var jid = $(this).attr('jid'); // The jabber_id of your contact
// You can probably put them in a unordered list and and use their jids as ids.
});
App.connection.addHandler(App.on_presence, null, "presence");
App.connection.send($pres());
}
on_presence
回调和connection.send($pres())
。它的目的是让您在联系人的在线状态发生更改时获得更新。您的在线状态回调将如下所示:on_presence(presence){
var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc...
var from = $(presence).attr('from'); // the jabber_id of the contact
if (presence_type != 'error'){
if (presence_type === 'unavailable'){
// Mark contact as offline
}else{
var show = $(presence).find("show").text(); // this is what gives away, dnd, etc.
if (show === 'chat' || show === ''){
// Mark contact as online
}else{
// etc...
}
}
}
return true;
}
您可以查看Strophe.js 文档获取更多信息。使用sendIQ,您可以添加更多参数,如错误回调等。
希望这有所帮助!
编辑:
请原谅我,我犯了一个错误。$(presence).attr('type')
不能告诉您联系人是否在线,而是提供了出席类型。实际上,出席类型是告诉您联系人是否不可用,以及您是否已订阅、取消订阅等的信号。
XMPP文档中:
2.2.1. 存在类型on_presence(presence){
var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc...
var from = $(presence).attr('from'); // the jabber_id of the contact
if (presence_type != 'error'){
if (presence_type === 'unavailable'){
// Mark contact as offline
}else{
var show = $(presence).find("show").text(); // this is what gives away, dnd, etc.
if (show === 'chat' || show === ''){
// Mark contact as online
}else{
// etc...
}
}
}
//RETURN TRUE!!!!!!!!!
return true;
}
return true
,正如下面的答案所指出的那样。 :) - Vincent Paca