我将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");
});
当我从其他客户端数据发送数据到达服务器但服务器没有将数据发送到客户端时。
提前致谢。
答案 0 :(得分:0)
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);
});
答案 1 :(得分:0)
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)
});
您正在将该节发送到它所来自的同一客户端。 将其发送给正确的接收方客户端 我已经在这里回答了。请参考