javascript - Strope 不使用 javascript 调用 iq addHandler

标签 javascript xmpp openfire strophe

我正在开发使用 Stropejs 与 XMPP openfire 连接的应用程序。问题是存在发送正确,但 iq 处理程序从未被调用。我检查了浏览器中的控制台,但没有发现错误。

我想发送状态并发送 iq,这将使客户端在 openfire 的客户端 session 中登录(10 秒内自动 session 关闭问题)。

这是我的js:

function onConnect(status) {
debugger;
if (status == Strophe.Status.CONNECTING) {
    alert('Strophe is connecting.');
    log('Strophe is connecting.');
} else if (status === Strophe.Status.AUTHENTICATING) {
    alert ('status AUTHENTICATING');
} else if (status === Strophe.Status.AUTHFAIL) {
    alert ('status AUTHFAIL');
} else if (status === Strophe.Status.ATTACHED) {
  alert ('status ATTACHED');
} else if (status == Strophe.Status.CONNFAIL) {
    alert('Strophe failed to connect.');
    log('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
    alert('Strophe is disconnecting.');
    log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
    alert('Strophe is disconnected.');
    log('Strophe is disconnected.');
    reConnectTimer = setInterval(reConnect, 3000);
} else if (status == Strophe.Status.CONNECTED) {
    connection.addHandler(onOwnMessage, null, 'iq', 'set', null, null);
    connection.addHandler(onMessage, null, 'message', null, null, null);
    connection.addHandler(on_presence, null, 'presence', null, null, null);
    connection.send($pres().tree());

    var pres = $pres({ to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b4a0abb6b6b499aaaca9a9b6abadf7b4a0abb6b6b4" rel="noreferrer noopener nofollow">[email protected]</a>/' + Math.random() });
    connection.send(pres);

    alert('Strophe is connected.');
    log('Strophe is connected.');

    clearInterval(reConnect);
    //connection.disconnect();
   }
}

function onOwnMessage(msg) {
debugger;
//  console.log(msg);
alert('msg is: ' + msg);
var elems = msg.getElementsByTagName('own-message');
if (elems.length > 0) {
    var own = elems[0];
    var to = $(msg).attr('to');
    var from = $(msg).attr('from');
    var iq = $iq({
        to: from,
        type: 'error',
        id: $(msg).attr('id')
    }).cnode(own).up().c('error', { type: 'cancel', code: '501' })
    .c('feature-not-implemented', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' });
    connection.sendIQ(iq);
    alert(iq);
}
return true;
}

请告诉我我做错了什么?我已经尝试并用谷歌搜索,但仍然无法解决。

提前致谢。

最佳答案

您的代码来自https://gist.github.com/RudyLu/5641350 ,这是使用 Strope.js 连接 Facebook 聊天的示例代码。 我猜您会基于您自己的 XMPP 服务器创建一个简单的聊天(例如,使用您帖子中标签所述的 Openfire)。 在这种情况下,您不需要 IQ 处理程序:

connection.addHandler(onOwnMessage, null, 'iq', 'set', null, null)

onMessage 和 on_presence 处理程序就足够了,可能类似于以下代码:

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));
    }
    // we must return true to keep the handler alive.  
    // returning false would remove it after it finishes.
    return true;
}

function on_presence (presence){
    console.log('onPresence:');
    var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc...
    var from = $(presence).attr('from'); // the jabber_id of the contact
    console.log('   >'+from+' --> '+presence_type);
    if (presence_type != 'error'){
        if (presence_type === 'unavailable'){
          // Mark contact as offline
        }else{
          var show = $(presence).find("show").text(); // this is what gives away, dnd, etc.
          if (show === 'chat' || show === ''){
            // Mark contact as online
          }else{
            // etc...
          }
        }
    }
    return true;
}

以下是发送消息和更改在线状态的函数示例:

function sendMessage(msg, to) {
    console.log('CHAT: Send a message to ' + to + ': ' + msg);
    var m = $msg({to: to, from: connection.jid, type: 'chat'}).c("body").t(msg);
    connection.send(m);
}

function setStatus(s) {
    console.log('setStatus: '+s);
    var status = $pres().c('show').t(s);
    connection.send(status); 
}

关于javascript - Strope 不使用 javascript 调用 iq addHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561931/

相关文章:

javascript - Accordion 部分,单击图像以旋转图标

javascript - 如果 Windows 上的条件加载在 javascript 中不起作用

javascript - 精简 jQuery

javascript - react 未捕获类型错误 : Cannot read property '__reactAutoBindMap' of undefined

ruby - 使用 ruby​​ xmpp4r 向离线 Google Talk 用户发送 XMPP 消息

ios - 当应用程序在后台时,如何在 IOS 应用程序中保持 XMPP 连接?

xcode - 需要一个解决方案来实现 xmpp 在群聊中添加 friend 并在 ios 中一次向他们发送消息

android - 如何从xmpp服务器smack库获取聊天记录

ios - 向 ios 发送推送通知以与离线用户聊天,openfire xmpp

protocols - 为什么 STUN 服务器需要两个不同的公共(public) IP 地址