如何为Strophe MUC插件添加onmessage处理程序

4

如何为Strophe MUC插件添加消息处理程序。

目前我已经为加入操作添加了回调函数。

Gab.connection.muc.join(room_name+"@muc.162.242.222.249",  login_id, 
function(message){ 
1个回答

4

你可以在通用消息处理程序中检查消息类型:

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;
}

谢谢回复。现在加入了这行代码,收到的消息可以正常工作了:connection.addHandler(Gab.on_message, null, "message", "groupchat"); - user1752065
现在我想要的下一件事是为用户提供消息历史记录(当用户点击加入的群组时)。目前我已经有了处理存档的以下代码。 connection.mam.query( Strophe.getBareJidFromJid(Gab.connection.jid), { with : jid+"@muc.server", max: 50, before: '', onMessage: function (message) { - user1752065

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接