在基于 Strophe.js 的聊天应用程序中处理状态 [英] Handling presence in Strophe.js-based chat application

查看:30
本文介绍了在基于 Strophe.js 的聊天应用程序中处理状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何现有的解决方案为基于 Strophe.js 的聊天应用程序提供状态处理?

Is there any existing solution which provides the presence handling for chat app based on Strophe.js?

我有一个基于 Strophe.js 的简单聊天应用程序.我只想显示在线并动态更改列表的用户.我想知道是否有任何现有的解决方案(可能是 Strophe 插件)来处理这个问题.如果没有这样的事情,实现它的最佳/最简单的方法是什么?

I have simple chat application based on Strophe.js. I'd like to show only the users who are online and dynamicaly alter the list. I was wondering whether there is any existing solution (possibly Strophe plugin) which handles this. If there's no such thing, what's the best/simplest way to implement it?

推荐答案

通过使用 Strophe,你可以像这样向你的服务器发送一个 IQ 来询问你的名单:

By using Strophe, you can just send an IQ to your server that asks for your roster list like so:

iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
App.connection.sendIQ(iq, your_roster_callback_function);

这将查询您的服务器以获取您的名册并返回一个包含您名册列表的对象.然后,您可以遍历您的名册,例如:

This would query your server for your rosters and would return an object containing your roster list. You can then iterate through your rosters like:

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()).它的目的是让您可以在联系人的状态发生变化时从他们那里获取更新.您的状态回调将如下所示:

Notice that I added an on_presence callback and connection.send($pres()). It's purpose is so that you can get updates from your contacts if ever their presence change. Your presence callback will then look like this:

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,您可以添加更多参数,例如错误回调等.

You can check the Strophe.js documentation for more information. With sendIQ you can add more parameters like error callbacks, etc.

希望这有帮助!

原谅我,我犯了一个错误.$(presence).attr('type') 不会告诉你联系人是否在线,但它会给你在线类型.在线状态实际上是一种信号,用于告诉您某个联系人是否不可用,或者您是否订阅、取消订阅等.

Forgive me, I made a mistake. $(presence).attr('type') doesn't give you if the contact is online or not but it gives you the presence type. The presence type is actually the signal that tells you if a contact if it is unavailable or if you are subscribed, unsubscribed, etc to your contact.

XMPP 文档中:

2.2.1.存在类型

存在节的类型"属性是可选的.一个存在不具有类型"属性的节用于向发件人在线并可进行通信的服务器.如果包含,'type' 属性指定缺乏可用性,请求管理对另一个实体的存在的订阅,一个请求另一个实体的当前存在,或与相关的错误一个先前发送的存在节.如果包含,'type' 属性必须具有以下值之一:

The 'type' attribute of a presence stanza is OPTIONAL. A presence stanza that does not possess a 'type' attribute is used to signal to the server that the sender is online and available for communication. If included, the 'type' attribute specifies a lack of availability, a request to manage a subscription to another entity's presence, a request for another entity's current presence, or an error related to a previously-sent presence stanza. If included, the 'type' attribute MUST have one of the following values:

  • unavailable -- 表示该实体不再可用沟通.
  • subscribe -- 发件人希望订阅收件人的存在.
  • subscribed -- 发件人已允许接收者接收他们的存在.
  • unsubscribe -- 发件人是取消订阅另一个实体的存在.
  • 取消订阅——订阅请求已被拒绝或先前已授予订阅已被取消.等等...

它是 $(presence).find("show") 给你你的联系人的状态.来自文档:

It is $(presence).find("show") gives you the status of your contact. From the docs:

2.2.2.1.显示

OPTIONAL 元素包含非人类可读的 XML 字符指定实体的特定可用性状态的数据或特定资源.一个存在节不得包含多个元素.该元素不得拥有任何属性.如果提供,XML 字符数据值必须是以下之一(额外的可用性类型可以通过存在节的正确命名空间的子元素):

The OPTIONAL element contains non-human-readable XML character data that specifies the particular availability status of an entity or specific resource. A presence stanza MUST NOT contain more than one element. The element MUST NOT possess any attributes. If provided, the XML character data value MUST be one of the following (additional availability types could be defined through a properly-namespaced child element of the presence stanza):

  • away -- 实体或资源暂时离开.
  • chat -- 实体或资源对聊天很感兴趣.
  • dnd -- 实体或资源正忙(dnd =请勿打扰").
  • xa -- 实体或资源离开一段时间(xa = "eXtended Away").

如果没有提供 show 元素,则假定该实体在线并且可用.

If no show element is provided, the entity is assumed to be online and available.

这篇关于在基于 Strophe.js 的聊天应用程序中处理状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆