如何为 strophe muc 插件添加 onmessage 处理程序
How to add onmessage handler for strophe muc plugin
如何为 strophe MUC 插件添加消息处理程序。
目前我添加了加入操作的回调函数。
Gab.connection.muc.join(room_name+"@muc.162.242.222.249", login_id,
function(message){
您可以在通用消息处理程序中检查消息类型:
connection.addHandler(onMessage, null, 'message', null, null, null);
...
function onMessage(msg) {
var to = msg.getAttribute('to');
var from = msg.getAttribute('from');
var type = msg.getAttribute('type');
var elems = msg.getElementsByTagName('body');
if (type == "chat" && elems.length > 0) {
var body = elems[0];
console.log('CHAT: I got a message from ' + from + ': ' + Strophe.getText(body));
} else if (type == "groupchat" && elems.length > 0) {
var body = elems[0];
var room = Strophe.unescapeNode(Strophe.getNodeFromJid(from));
var nick = Strophe.getResourceFromJid(from);
console.log('GROUP CHAT: I got a message from ' + nick + ': ' + Strophe.getText(body) + ' in room: ' + room);
}
// we must return true to keep the handler alive.
// returning false would remove it after it finishes.
return true;
}
如何为 strophe MUC 插件添加消息处理程序。
目前我添加了加入操作的回调函数。
Gab.connection.muc.join(room_name+"@muc.162.242.222.249", login_id,
function(message){
您可以在通用消息处理程序中检查消息类型:
connection.addHandler(onMessage, null, 'message', null, null, null);
...
function onMessage(msg) {
var to = msg.getAttribute('to');
var from = msg.getAttribute('from');
var type = msg.getAttribute('type');
var elems = msg.getElementsByTagName('body');
if (type == "chat" && elems.length > 0) {
var body = elems[0];
console.log('CHAT: I got a message from ' + from + ': ' + Strophe.getText(body));
} else if (type == "groupchat" && elems.length > 0) {
var body = elems[0];
var room = Strophe.unescapeNode(Strophe.getNodeFromJid(from));
var nick = Strophe.getResourceFromJid(from);
console.log('GROUP CHAT: I got a message from ' + nick + ': ' + Strophe.getText(body) + ' in room: ' + room);
}
// we must return true to keep the handler alive.
// returning false would remove it after it finishes.
return true;
}