javascript - Node xmpp 消息未发送到正确的目的地

标签 javascript node.js node-xmpp

我正在使用 node-xmpp 服务器作为 2 个客户端的聊天服务器:Psi 和 Spark。我为每个客户端注册一个帐户。我想从 Psi 帐户发送一条消息到 Spark 帐户。每次我发送来自一个帐户的消息同一帐户接收消息。我想将 friend 添加到我的列表中(我不知道为什么不起作用..可能没有实现)并正确发送消息。我正在使用 Node xmpp 服务器/examples/server-and-client.js。感谢您的建议。

最佳答案

这个完整的代码对我来说工作得很好:

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

function generateRoster(jid, name, id, to) {
    // This is a roster request, we create the response node
    var roster = new xmpp.Element('iq', {
        id: id,
        to: to,
        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: jid, // We send the jid and the name
            name: name,
            subscription: 'both'
        }).c('group').t('Connected Clients'); // We add it to the 'Connected Clients' group

    return roster;
}

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

    var connectedUsers = [];
    var clientsHandles = {};

    // On connection event. When a client connects.
    server.on('connection', function (client) {
        var userJid = null;
        // 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 () {
            userJid = client.jid.user + '@' + client.jid.domain + '/' + client.jid.resource;
            console.log(userJid + 'ONLINE');

            for (var i = 0; i < connectedUsers.length; i++) {
                var myRoster = generateRoster(userJid, userJid, 'myRandomId', connectedUsers[i]);

                if (clientsHandles[connectedUsers[i]]) {
                    clientsHandles[connectedUsers[i]].send(myRoster);
                    console.log("Sending my new infos to ", connectedUsers[i]);
                }
            }

            connectedUsers.push(userJid);
            client.jid.userJid = userJid;
            clientsHandles[userJid] = client;
        });

        // Stanza handling
        client.on('stanza', function (stanza) {
            if (stanza.is('message') && (stanza.attrs.type !== 'error')) {
                if (clientsHandles[stanza.attrs.to]) {
                    clientsHandles[stanza.attrs.to].send(stanza);
                }
            }
            else if (stanza.is('presence')) {
                // We loop through the user list
                for (var j = 0; j < connectedUsers.length; j++) {
                    console.log(stanza.toString());
                    var jid = connectedUsers[j];
                    stanza.to = jid;
                    clientsHandles[jid].send(stanza);
                }
            }
            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 loop through the user list
                        for (var j = 0; j < connectedUsers.length; j++) {

                            var roster = generateRoster(connectedUsers[j], connectedUsers[j], stanza.attrs.id, stanza.attrs.from);
                            client.send(roster); // We send it back to the client
                        }

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

        // On Disconnect event. When a client disconnects
        client.on('disconnect', function () {
            if (userJid) {
                console.log(userJid, "DISCONNECTED");
                connectedUsers.splice(connectedUsers.indexOf(userJid), 1);
                delete clientsHandles[userJid];
            }
        });

    });

    server.on('listening', done)
};

startServer(function () {
    console.log("Server running");
});

当消息节到达时,我检查接收器是否已连接,如果是,我将节发送给它。

关于javascript - Node xmpp 消息未发送到正确的目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32135501/

相关文章:

node.js - 如何从 AWS lambda 发布到 Node.js 中的云观察指标

javascript - 如何让 Guard 自动编译我的 Coffeescript 文件?说是但不是

javascript - Electron + xmpp.js : How I can Debug SSL Self-signed error messages?

javascript - Canvas 的 ToDataURL (JavaScript) 在通过 PHP 上传到服务器后创建损坏的 PNG 文件

Node.JS Socket.IO SSL CORS 错误

mysql - Node.js xmpp 项目连接 MySQL

xmpp - 有人有安装 node-xmpp 的更新指南吗?

javascript - 无法添加 Phaser JS 状态

javascript - 几次击键后打开 react-select 自动完成

javascript - 链接组件在位置相同时更新浏览器历史记录