handler - 如何为 strope muc 插件添加 onmessage 处理程序

标签 handler strophe multiuserchat

如何为 strope 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;
}

关于handler - 如何为 strope muc 插件添加 onmessage 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38455003/

相关文章:

android - 处理程序 postDelayed - 每秒打印一些东西

c# - 事件发送者在客户端的事件处理程序代码中被处置

javascript - 如何/资源配置 Openfire 及其内置的 Bosh 支持以接受 XMPP 聊天客户端?

javascript - 保存从 xmpp Strophe si-filetransfer 收到的文件

xmpp - 为什么 XMPP 服务器忽略我的昵称订阅请求?

ios - 使用 xmppFramework 在 ejabberd 服务器上实现 MUC

java - NettyIO 未正确删除 channel

c# - C# WinForms 应用程序中的应用程序范围的消息处理程序

xmpp - ejabberd : what is the best? 中数百万个私有(private) MUC 房间或动态 XEP-0033 对话

xmpp - 发现 MUC 房间的成员作为占用者