节点xmpp服务器无法正确发送消息

时间:2015-08-20 08:36:10

标签: node.js xmpp

我有以下node-xmpp服务器。在这个服务器上,我连接2个客户端并互相发送消息。在控制台中我想看到收到的消息,但我看到我发送的消息。任何想法? 这是代码:

服务器:

'use strict'

var xmpp = require('../index')


 , server = null
  , Client = require('node-xmpp-client')
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')
        cb(true)
    })

    // 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('server:', client.jid.local, '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
        client.send(stanza)
        //console.log('Stanza sent is :'+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() {
   })

客户代码: 客户端1:

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

// On Connect event. When a client connects.

client = new xmpp.Client({jid: 'admin@localhost', password: 'secret'});

client.addListener("authenticate", function(opts, cb) {
    console.log("AUTH" + opts.jid + " -> " +opts.password); 
    cb(null, opts); 
});

client.addListener('error', function(err) {
    console.log(err.toString());
});

client.addListener('online', function() {
    console.log("online");
    var stanza1 = new xmpp.Element('message', { to: 'admin6@localhost', type: 'chat', 'xml:lang': 'ko' }).c('body').t('aaaaaMessage from admin');
    //setInterval(sender,1000);
  client.send(stanza1);

});

//client.on("stanza", function(stanza) {
    //console.log("STANZA" + stanza);
   // console.log('S-a primit ceva: '+stanza);
//});
client.on('stanza',function(message){
    console.log('AAAA '+message);
})
client.addListener("disconnect", function(client) {
    console.log("DISCONNECT");
});

CLient2:

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

// On Connect event. When a client connects.

client = new xmpp.Client({jid: 'admin6@localhost', password: 'secret'});

client.addListener("authenticate", function(opts, cb) {
    console.log("AUTH" + opts.jid + " -> " +opts.password); 
    cb(null, opts); 
});

client.addListener('error', function(err) {
    console.log(err.toString());
});

client.addListener('online', function() {
    console.log("online");
    var stanza = new xmpp.Element('message', { to: 'admin@localhost', type: 'chat', 'xml:lang': 'ko' }).c('body').t('aaaaaMessage from admin6');
    //setInterval(sender,1000);
  client.send(stanza);

});

client.on("stanza", function(stanza) {
    console.log("STANZA" + stanza);
    //console.log('S-a primit ceva: '+stanza);
});
//client.addListener('stanza',function(message){
   // console.log('AAAA '+message);
//})
client.addListener("disconnect", function(client) {
    console.log("DISCONNECT");
});

0 个答案:

没有答案