【问题标题】:How to add contacts in node xmpp roster?如何在节点 xmpp 名册中添加联系人?
【发布时间】:2015-08-25 07:59:53
【问题描述】:

我正在使用 node-xmpp-server 作为聊天应用程序的 xmpp 服务器。作为客户端,我正在使用 Spark。我想将联系人添加到名册。我该怎么做?我在 ejabbberd 上运行并且它可以工作,但我需要实现一个代码。感谢您的任何建议!

我的服务器代码:

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



})

【问题讨论】:

    标签: javascript node.js xmpp


    【解决方案1】:

    这是一个超级基础的例子,你可以随意扩展:

    首先,您需要查找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 属性等于jabber:iq:roster 的节点:

    ...
    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 个“假”用户并将他们发送给用户,你可以将这种循环应用到一个真实的用户列表中。

    小截图:

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

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

    希望对你有帮助。

    【讨论】:

    • 我会尝试 ant 如果它有效我会像一个很好的答案一样匹配..我看到你熟悉 xmpp..你能看看这个问题吗? stackoverflow.com/questions/32135501/…
    • @Worker1 是的,请稍等
    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 2012-05-20
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    相关资源
    最近更新 更多