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

Javascript 如何在节点xmpp花名册中添加联系人?,javascript,node.js,xmpp,Javascript,Node.js,Xmpp,我正在使用node xmpp server作为聊天应用程序的xmpp服务器。作为客户端,我正在使用Spark。我想将联系人添加到花名册中。我如何才能做到这一点?我已经在Ejabberd和iti上运行过,但我需要实现一个代码。感谢您的建议 我的服务器代码: var startServer = function(done) { // Sets up the server. server = new xmpp.C2S.TCPServer({ port: 5222,

我正在使用node xmpp server作为聊天应用程序的xmpp服务器。作为客户端,我正在使用Spark。我想将联系人添加到花名册中。我如何才能做到这一点?我已经在Ejabberd和iti上运行过,但我需要实现一个代码。感谢您的建议

我的服务器代码:

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() {



})

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

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

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);
    }
});
现在,我们必须循环遍历此请求的所有子节点,并查找具有
名称
属性equal
查询
xmlns
属性equal
jabber:iq:花名册
的节点:

...
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
...
。。。
else if(stanza.is('iq')&&stanza.attrs.type=='get'){
for(变量i=0;i
在本例中,我创建了5个“假”用户并将其发送给用户,例如,您可以将这种循环应用于真实的用户列表

一个小屏幕截图:

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

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


希望有帮助。

这是一个超级基本的示例,您可以根据需要扩展它:

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

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);
    }
});
现在,我们必须循环遍历此请求的所有子节点,并查找具有
名称
属性equal
查询
xmlns
属性equal
jabber:iq:花名册
的节点:

...
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
...
。。。
else if(stanza.is('iq')&&stanza.attrs.type=='get'){
for(变量i=0;i
在本例中,我创建了5个“假”用户并将其发送给用户,例如,您可以将这种循环应用于真实的用户列表

一个小屏幕截图:

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

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


希望有帮助。

如果ant有效,我会尝试使用它,我会找到一个很好的答案。.我看你对xmpp很熟悉。.你能看看这个问题吗@工作人员1是的,给我一点时间我会试试ant如果它有效的话我会很好的匹配答案..我看你对xmpp很熟悉..你能看看这个问题吗@工人1是的,给我一点时间