4

node-xmpp モジュールを使用して XMPP サーバーに接続し、グループ チャットに参加しています。サーバーへの接続、プレゼンスの設定、ルームへの参加、メッセージの読み上げは今のところ機能しています。しかし、部屋のユーザーリストも受け取りたいです。

XMPP プロトコルでは、クライアントがルームに入るときにプレゼンス スタンザを送信する必要があります ( http://xmpp.org/extensions/xep-0045.html#enter-pres )。しかし、どうすればノードで解析できますか?

私のコードは現在次のようになっています。

var xmpp = require('node-xmpp');

// Create the XMPP Client
var cl = new xmpp.Client({
    jid: jid,
    password: password,
    reconnect: true
});

// Do things when online
cl.on('online', function() {
  util.log("We're online!");

  // Set client's presence
  cl.send(new xmpp.Element('presence', { type: 'available' }).c('show').t('chat'));
  cl.send(new xmpp.Element('presence', { to: room_jid+'/'+room_nick }).c('x', { xmlns: 'http://jabber.org/protocol/muc' }).c('history', {seconds: 1}));

  // Send keepalive
  setInterval(function() {
    cl.send(' ');
  }, 30000);




  cl.on('stanza', function(stanza) {
      // always log error stanzas
      if (stanza.attrs.type == 'error') {
        util.log('[error] ' + stanza);
        return;
      }

      // ignore everything that isn't a room message
      if (!stanza.is('message') || !stanza.attrs.type == 'chat') {
        return;
      }

      var body = stanza.getChild('body');
      // message without body is probably a topic change
      if (!body) {
        return;
      }

    // Extract username
    var from, room, _ref;
    _ref = stanza.attrs.from.split('/'), room = _ref[0], from = _ref[1];
     var message = body.getText();

     // Log topics and messages to the console
     if(!from) {
        util.log('Topic: ' + message);
     } else {
        util.log('[' + from + ']: ' + message);
     }
    });
});

私はすでに使用してプレゼンスをトリガーしようとしました

if(stanza.is('presence')) {}

cl.on('stanza') 部分内ですが、機能しません。

4

1 に答える 1

4

UPDATE : クライアントがリクエストを送信する必要のない新しいメソッドについて説明しています。

背景: クライアントがグループ チャットに参加すると、サーバーは、接続されているユーザーに関する情報を含むプレゼンス スタンザをグループ チャットに返します。

cl.on('stanza', function(stanza) {
    // always log error stanzas
    if (stanza.attrs.type == 'error') {
        util.log('[error] ' + stanza);
        return;
    }

    if(stanza.is('presence')){
        // We are only interested in stanzas with <x> in the payload or it will throw some errors
        if(stanza.getChild('x') !== undefined) {
            // Deciding what to do based on the xmlns attribute
            var _presXmlns = stanza.getChild('x').attrs.xmlns;

            switch(_presXmlns) {
                // If someone is joining or leaving
                case 'http://jabber.org/protocol/muc#user':
                    // Get the role of joiner/leaver
                    _presRole = stanza.getChild('x').getChild('item').attrs.role;
                    // Get the JID of joiner/leaver
                    _presJID  = stanza.getChild('x').getChild('item').attrs.jid;
                    // Get the nick of joiner/leaver
                    _presNick = stanza.attrs.from.split('/')[1];


                    // If it's not none, this user must be joining or changing his nick
                    if(_presRole !== 'none') {

                        // We are now handling the data of joinging / nick changing users. I recommend to use an in-memory store like 'dirty' [https://github.com/felixge/node-dirty] to store information of the users currentliy in the group chat.


                    } else {

                        // We are now handling the data of leaving users

                    }
                break;
            }

            return;
        }

        return;
    }

古い方法

グループ チャットで現在のユーザーをサーバーに照会する方法については、以前に説明しました。すべてのユーザー トラフィック (参加、脱退、ニックネームの変更) が保存されるストアを維持することで、これは不要になります。ただし、プレゼンススタンザがクライアントに正しく配信されなかったなどの問題によって、データの一貫性を確保するために引き続き使用できます。それが、以下で説明されている理由です。

ルームに接続しているユーザーのリストをリクエストするには、次の操作を実行する必要があります。

最初にサーバーにリクエストを送信し、ユーザー リストを要求します。

cl.send(new xmpp.Element('iq', {from: jid, to: room_jid, type: 'get' }).c('query', { xmlns: 'http://jabber.org/protocol/disco#items' }));

次に、iq スタンザをリッスンし、それらを解析して、配列にデータを入力します。

// Catching the requested user list
if(stanza.is('iq')){
    // Fetching usernames from return data (data structure: http://xmpp.org/extensions/xep-0045.html#example-12)
    var _items = stanza.getChild('query').getChildren('item');
    var users = new Array();
    for(var i = 0; i<_items.length; i++) {
        // We are building an object here to add more data later
        users[i] = new Object();
        users[i]['name'] = _items[i].attrs.name;
    }
    console.log(util.inspect(users, {depth: null, colors: true}));
    return;
}

これにより、ユーザー リストが表示されます。一意の JID を要求するには、すべてのユーザーを調査する必要があります。リストを最新の状態に保つには、ユーザーが離れるときにユーザーを削除し、参加するときに + プローブを追加する必要があります。

于 2013-06-17T01:27:14.907 に答える