javascript - How to add contacts in node xmpp roster? -
i'm using node-xmpp-server xmpp server chat application.as client i'm using spark.i want add contacts roster.how can this?i've run on ejabbberd , iti work need implement code.thanks advice!
my server code:
var startserver = function(done) { // sets server. server = new xmpp.c2s.tcpserver({ port: 5222, domain: 'localhost' }) // on connection event. when client connects. server.on('connection', function(client) { // that's way add mods given server. // allows developer register jid against want client.on('register', function(opts, cb) { console.log('register') console.log(cb); cb(false) }) // allows developer authenticate users against 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 = stanza.attrs.from // stanza.attrs.from = stanza.attrs.to //stanza.attrs.to = if (stanza.is('message') && (stanza.attrs.type !== 'error')) { client.send(stanza); } else { client.send(stanza); } }) // on disconnect event. when client disconnects client.on('disconnect', function() { console.log('server:', client.jid.local, 'disconnect') }) }) server.on('listening', done) } startserver(function() { })
this super basic example, can extend want:
first of all, need iq
stanzas, get
type add:
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); } });
we have loop on children of request, , node name
attribute equal query
, , xmlns
attribute equal jabber:iq:roster
:
... else if (stanza.is('iq') && stanza.attrs.type == 'get') { (var = 0; < stanza.children.length; i++) { if (stanza.children[i].name == 'query' && stanza.children[i].attrs.xmlns == 'jabber:iq:roster') { // create 5 fake users (var j = 0; j < 5; j++) { // roster request, create response node var roster = new xmpp.element('iq', { id: stanza.attrs.id, // copy id to: stanza.attrs.from, // send sender type: 'set' }); roster.c('query', {xmlns: 'jabber:iq:roster', ver: 'ver13'})// add children tag `query`, 2 attribute xmlns , ver .c('item', { // add children 'item' jid: 'name' + j + '@test.com', // create jid name: 'user ' + j, // generate name subscription: 'both' }).c('group').t('yourgroup'); // add 'yourgroup' group client.send(roster); // send client } } } client.send(stanza); } else ...
in example, created 5 'fake' users , sent them user, can apply kind of loop real user list instance.
a little screenshot:
as can see, create 'yourgroup' group, , adds 5 users it.
when try start conversation 1 of users, can see correctly generated jid in top bar.
hope helps.
Comments
Post a Comment