我正在使用node-xmpp模块连接XMPP服务器并加入群聊。目前已经成功连接到服务器,设置了出席状态,加入了房间并读取消息。但我也想接收房间的用户列表。
根据XMPP协议,在客户端进入房间时需要发送出席元素(http://xmpp.org/extensions/xep-0045.html#enter-pres)。但是,我该如何在node中解析它呢?
我的代码目前看起来像这样:
根据XMPP协议,在客户端进入房间时需要发送出席元素(http://xmpp.org/extensions/xep-0045.html#enter-pres)。但是,我该如何在node中解析它呢?
我的代码目前看起来像这样:
var xmpp = require('node-xmpp');
// Create the XMPP Client
var cl = new xmpp.Client({
jid: jid,
password: password,
reconnect: true
});
// Do things when online
cl.on('online', function() {
util.log("We're online!");
// Set client's presence
cl.send(new xmpp.Element('presence', { type: 'available' }).c('show').t('chat'));
cl.send(new xmpp.Element('presence', { to: room_jid+'/'+room_nick }).c('x', { xmlns: 'http://jabber.org/protocol/muc' }).c('history', {seconds: 1}));
// Send keepalive
setInterval(function() {
cl.send(' ');
}, 30000);
cl.on('stanza', function(stanza) {
// always log error stanzas
if (stanza.attrs.type == 'error') {
util.log('[error] ' + stanza);
return;
}
// ignore everything that isn't a room message
if (!stanza.is('message') || !stanza.attrs.type == 'chat') {
return;
}
var body = stanza.getChild('body');
// message without body is probably a topic change
if (!body) {
return;
}
// Extract username
var from, room, _ref;
_ref = stanza.attrs.from.split('/'), room = _ref[0], from = _ref[1];
var message = body.getText();
// Log topics and messages to the console
if(!from) {
util.log('Topic: ' + message);
} else {
util.log('[' + from + ']: ' + message);
}
});
});
我已经尝试使用以下方式触发出席状态:
if(stanza.is('presence')) {}
我尝试在cl.on('stanza')部分内实现,但没有成功。