使用模塊:npm install node-xmpp
var XMPP = require('node-xmpp');
var xmpp = new XMPP.Client({
jid : 'jid',
password : '123456',
host:'xxxx',
port : 5222
});
xmpp.on('error', function(err) {
console.error(err);
});
xmpp.on('online', function() {
console.log('Yes, I\'m connected!');
console.log('-------------------------');
xmpp.send('<iq id="vcard00" type="get"><vCard xmlns="vcard-temp"></vCard></iq>');
});
xmpp.on('stanza', function(stanza) {
console.log(stanza.toString());
if(stanza.is('iq') && stanza.attrs.type !=='error' ) {
...............
} else if(stanza.attrs.type =='error'){
callback('upd vcard error!')
}
})
//構造xml Element對象:
var data = new XMPP.Element('iq',{id:'change00',type:'set',to:name[1]})
.c('query',{xmlns:'jabber:iq:register'}) //創建新結點
.c('username').t(name[0]) //創建新結點並賦值
.up() //回到上層結點
.tree(); //獲取根節點
var tel = data.getChild('vCard').getChild('TEL');
tel.text('test'); //給已有結點賦值
var str = new XMPP.Element('iq',{type:'set', id:'vcard01'})
.cnode(data.getChild('vCard')) //將data的vCard結點作爲子結點
.tree();