如何在节点 xmpp 名册中添加联系人?

How to add contacts in node xmpp roster?

我正在使用 node-xmpp-server 作为聊天的 xmpp 服务器 application.As 我正在使用的客户端 Spark.I 想将联系人添加到 roster.How 我可以吗这个?我已经 运行 在 ejabberd 和 iti 工作,但我需要实施 code.Thanks 以获得任何建议!

我的服务器代码:

var startServer = function(done) {
    // Sets up the server.
    server = new xmpp.C2S.TCPServer({
        port: 5222,
        domain: 'localhost'
    })

    // On connection event. When a client connects.
    server.on('connection', function(client) {
        // That's the way you add mods to a given server.

        // Allows the developer to register the jid against anything they want
        client.on('register', function(opts, cb) {
            console.log('REGISTER')
            console.log(cb);
            cb(false)
        })

        // Allows the developer to authenticate users against anything they want.
        client.on('authenticate', function(opts, cb) {
            //console.log('server:', opts.username, opts.password, 'AUTHENTICATING')
            if (opts.password === 'secret') {
                //console.log('server:', opts.username, 'AUTH OK')
                cb(null, opts)
            }
            else {
                //console.log('server:', opts.username, 'AUTH FAIL')
                cb(false)
            }
        })

        client.on('online', function() {
            console.log('ONLINE')
        })

        // Stanza handling
        client.on('stanza', function(stanza) {
            console.log('server:', client.jid.local, 'stanza', stanza.toString())
            //var from = stanza.attrs.from
           // stanza.attrs.from = stanza.attrs.to
            //stanza.attrs.to = from
           if (stanza.is('message') && (stanza.attrs.type !== 'error')) {

            client.send(stanza);
           }
           else {
            client.send(stanza);
        }
        })

        // On Disconnect event. When a client disconnects
        client.on('disconnect', function() {
            console.log('server:', client.jid.local, 'DISCONNECT')
        })

    })

    server.on('listening', done)
}

startServer(function() {



})

这是一个超级基础的示例,您可以根据需要对其进行扩展:

首先,您需要查找 iq 节,类型为 get 因此我们添加:

client.on('stanza', function (stanza) {
    console.log('server:', client.jid.local, 'stanza', stanza.toString())
    if (stanza.is('message') && (stanza.attrs.type !== 'error')) {
        client.send(stanza);
    }
    else if (stanza.is('iq') && stanza.attrs.type == 'get') {
        ...
    }
    else {
        client.send(stanza);
    }
});

我们现在必须遍历此请求的所有子节点,并寻找 name 属性等于 queryxmlns 属性等于 [=17] 的节点=]:

...
else if (stanza.is('iq') && stanza.attrs.type == 'get') {
    for (var i = 0; i < stanza.children.length; i++) {
        if (stanza.children[i].name == 'query' && stanza.children[i].attrs.xmlns == 'jabber:iq:roster') {

            // We create 5 fake users    
            for (var j = 0; j < 5; j++) {

                // This is a roster request, we create the response node
                var roster = new xmpp.Element('iq', {
                    id: stanza.attrs.id, // We copy the ID
                    to: stanza.attrs.from, // We send it back to the sender
                    type: 'set'
                });

                roster.c('query', {xmlns: 'jabber:iq:roster', ver: 'ver13'})// We add a children tag `query`, with the two attribute xmlns and ver
                    .c('item', { // We add a children 'Item'
                        jid: 'name' + j + '@test.com', // We create the jid
                        name: 'User ' + j, // We generate its name
                        subscription: 'both'
                    }).c('group').t('YourGroup'); // We add it to the 'YourGroup' group

                client.send(roster); // We send it back to the client
            }

        }
    }
    client.send(stanza);
}
else
...

在这个例子中,我创建了 5 'fake' 个用户并将它们发送给用户,例如您可以将这种循环应用于真实的用户列表。

一张小截图:

如您所见,它创建了一个 'YourGroup' 组,并将所有 5 个用户添加到其中。

当您尝试与其中一位用户开始对话时,您可以在顶部栏中看到正确生成的 jid。

希望对您有所帮助。