【问题标题】:node-xmpp send data from One client to anothernode-xmpp 将数据从一个客户端发送到另一个客户端
【发布时间】:2015-09-04 08:28:21
【问题描述】:

我将 node-XMPP 用于聊天应用程序,其中两个客户端可以互相聊天。但问题是我正在将数据从一个客户端发送到服务器,但我不知道如何将数据从服务器发送到另一个客户端。
我的服务器代码是:

'use strict';
    var xmpp = require('../index')
      , c2s = null
      , Client = require('node-xmpp-client')
      , ltx = require('node-xmpp-core').ltx
      , util = require('util');

    var startServer = function(done) {
        c2s = new xmpp.C2SServer({
            port: 5222,
            domain: 'localhost',
            preferred: 'PLAIN'
        })
        c2s.on('connect', function(client) {
           c2s.on('register', function(opts, cb) {
                console.log("REGISTER");
                cb(true);
            });
            client.on('authenticate', function(opts, cb) {
                if ('secret' === opts.password) {
                    return cb(null, opts)
                }
                console.log('FAIL')
                cb(false)
            });

            client.on('online', function() {
                console.log('ONLINE')
            });
            client.on('stanza', function(stanza) {
                console.log('STANZA', stanza.root().toString())
                 var from = stanza.attrs.from
                 stanza.attrs.from = stanza.attrs.to
                 stanza.attrs.to = from
                 client.send(stanza)
            });
            client.on('disconnect', function(jid) {
               console.log("client DISCONNECT=>");
            });
        });
         if (done) done()
    };

    startServer(function() {})

我的客户代码是:

   var Client = require('../index.js')
        ,util = require('util');

    var client = new Client({
        jid: process.argv[2],
        password: process.argv[3],
        host :"localhost",
        port:5222,
        reconnect :true
    });

    client.on('online', function() {
        console.log('online');
    });
    client.on('error',function(err){
        console.log("error=>"+err);
    });

    client.on('stanza', function(stanza) {
        console.log('Incoming stanza: ', stanza.toString())
    });

    client.on('disconnect',function(){
        console.log("server diconnected");
    });

当我从另一个客户端发送数据时,数据会到达服务器,但服务器没有将数据发送到客户端。
提前致谢。

【问题讨论】:

    标签: node.js xmpp ejabberd node-xmpp


    【解决方案1】:
    client.on('online', function() {
    console.log("online");
    var stanza = new xmpp.Element('message', { to: 'admin@localhost', type: 'chat', 'xml:lang': 'ko' }).c('body').t('aaaaaMessage from admin1');
    //setInterval(sender,1000);
    
    client.send(stanza);
    console.log(stanza);
    

    });

    【讨论】:

      【解决方案2】:
      client.on('stanza', function(stanza) {
                  console.log('STANZA', stanza.root().toString())
                   var from = stanza.attrs.from
                   stanza.attrs.from = stanza.attrs.to
                   stanza.attrs.to = from
            --->   client.send(stanza)
      

      });

      你正在将节发送到它来自的同一个客户端。 将其发送到正确的接收方客户端。
      我已经在这里回答了。请参考

      Node xmpp messages are not sent to correct destination

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-09
        • 1970-01-01
        • 2015-11-06
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多