Node.js 使用节点处理XMPP的存在
我正在使用节点xmpp模块连接到xmpp服务器并加入群组聊天。到目前为止,连接到服务器、设置状态、加入房间和读取消息都很有效。但是我也想收到房间的用户列表 XMPP协议要求在客户端进入房间时发送状态节()。但我现在如何在node中解析它呢 我的代码当前如下所示:Node.js 使用节点处理XMPP的存在,node.js,xmpp,node-xmpp,Node.js,Xmpp,Node Xmpp,我正在使用节点xmpp模块连接到xmpp服务器并加入群组聊天。到目前为止,连接到服务器、设置状态、加入房间和读取消息都很有效。但是我也想收到房间的用户列表 XMPP协议要求在客户端进入房间时发送状态节()。但我现在如何在node中解析它呢 我的代码当前如下所示: var xmpp = require('node-xmpp'); // Create the XMPP Client var cl = new xmpp.Client({ jid: jid, password: pas
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')部分中,但它不起作用。更新:我现在描述一种新方法,它不需要客户端发送请求 背景:当客户端加入群聊时,服务器会返回状态节,其中包含群聊中已连接用户的信息
cl.on('stanza', function(stanza) {
// always log error stanzas
if (stanza.attrs.type == 'error') {
util.log('[error] ' + stanza);
return;
}
if(stanza.is('presence')){
// We are only interested in stanzas with <x> in the payload or it will throw some errors
if(stanza.getChild('x') !== undefined) {
// Deciding what to do based on the xmlns attribute
var _presXmlns = stanza.getChild('x').attrs.xmlns;
switch(_presXmlns) {
// If someone is joining or leaving
case 'http://jabber.org/protocol/muc#user':
// Get the role of joiner/leaver
_presRole = stanza.getChild('x').getChild('item').attrs.role;
// Get the JID of joiner/leaver
_presJID = stanza.getChild('x').getChild('item').attrs.jid;
// Get the nick of joiner/leaver
_presNick = stanza.attrs.from.split('/')[1];
// If it's not none, this user must be joining or changing his nick
if(_presRole !== 'none') {
// We are now handling the data of joinging / nick changing users. I recommend to use an in-memory store like 'dirty' [https://github.com/felixge/node-dirty] to store information of the users currentliy in the group chat.
} else {
// We are now handling the data of leaving users
}
break;
}
return;
}
return;
}
然后听iq节,解析它们并用数据填充数组:
// Catching the requested user list
if(stanza.is('iq')){
// Fetching usernames from return data (data structure: http://xmpp.org/extensions/xep-0045.html#example-12)
var _items = stanza.getChild('query').getChildren('item');
var users = new Array();
for(var i = 0; i<_items.length; i++) {
// We are building an object here to add more data later
users[i] = new Object();
users[i]['name'] = _items[i].attrs.name;
}
console.log(util.inspect(users, {depth: null, colors: true}));
return;
}
//捕获请求的用户列表
if(第is节('iq')){
//从返回数据获取用户名(数据结构:http://xmpp.org/extensions/xep-0045.html#example-12)
var_items=stanza.getChild('query').getChildren('item');
var users=新数组();
对于(var i=0;我不确定您的问题的实际答案是什么……但您在这里描述的内容似乎很可怕。所有房间占用者都会收到进出房间的用户的状态信息。您为什么需要发送迪斯科查询?@MattJ您是对的。我找到了一种方法,可以在脚本进入房间时以及何时使用服务器发送的状态信息用户加入/离开。我会在有时间的时候更新我的帖子。但是使用iq查询的方法对于确保客户端维护正确的用户列表仍然很有用,例如,当状态消息未送达时。
// Catching the requested user list
if(stanza.is('iq')){
// Fetching usernames from return data (data structure: http://xmpp.org/extensions/xep-0045.html#example-12)
var _items = stanza.getChild('query').getChildren('item');
var users = new Array();
for(var i = 0; i<_items.length; i++) {
// We are building an object here to add more data later
users[i] = new Object();
users[i]['name'] = _items[i].attrs.name;
}
console.log(util.inspect(users, {depth: null, colors: true}));
return;
}