javascript 在基于 Strophe.js 的聊天应用程序中处理状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12062950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:13:23  来源:igfitidea点击:

Handling presence in Strophe.js-based chat application

javascriptxmppstrophe

提问by Pavel S.

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

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

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

回答by Vincent Paca

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

通过使用 Strophe,您可以向您的服务器发送一个 IQ 来询问您的名单,如下所示:

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());
}

Notice that I added an on_presencecallback 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回调和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;
}

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

您可以查看 Strophe.js文档以获取更多信息。使用 sendIQ,您可以添加更多参数,例如错误回调等。

Hope this helps!

希望这可以帮助!

Edit:

编辑:

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.

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

In the XMPP Documentation:

XMPP 文档中

2.2.1. Types of Presence

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 -- Signals that the entity is no longer available for communication.
  • subscribe -- The sender wishes to subscribe to the recipient's presence.
  • subscribed -- The sender has allowed the recipient to receive their presence.
  • unsubscribe -- The sender is unsubscribing from another entity's presence.
  • unsubscribed -- The subscription request has been denied or a previously-granted subscription has been cancelled. etc...

2.2.1. 存在类型

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

  • 不可用——表示该实体不再可用于通信。
  • subscribe -- 发件人希望订阅收件人的在线状态。
  • subscribed -- 发件人已允许收件人接收他们的出席信息。
  • 取消订阅——发件人正在取消订阅另一个实体的存在。
  • unsubscribed -- 订阅请求已被拒绝或先前授予的订阅已被取消。等等...

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

它为$(presence).find("show")您提供联系人的状态。从文档:

2.2.2.1. Show

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 -- The entity or resource is temporarily away.
  • chat -- The entity or resource is actively interested in chatting.
  • dnd -- The entity or resource is busy (dnd = "Do Not Disturb").
  • xa -- The entity or resource is away for an extended period (xa = "eXtended Away").

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

2.2.2.1. 展示

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

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

如果未提供 show 元素,则假定该实体在线且可用。

回答by RamRovi

One important thing to note, is that as Is it right that Strophe.addHandler reads only first node from response?said, If you want to read more than just the first presence node, make sure to return true at the end, because: "The handler should return true if it is to be invoked again; returning false will remove the handler after it returns."

需要注意的一件重要事情是,Strophe.addHandler 仅从响应中读取第一个节点是否正确?说,如果你想读取的不仅仅是第一个存在节点,一定要在最后返回 true,因为:“如果要再次调用处理程序,它应该返回 true;返回 false 将在它返回后删除处理程序。 ”

So the solution I used should look something 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!!!!!!!!!
   return true;
}