首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Strophe不使用javascript调用iq addHandler

Strophe不使用javascript调用iq addHandler
EN

Stack Overflow用户
提问于 2015-11-06 07:54:38
回答 1查看 1.3K关注 0票数 2

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

我想发送存在和发送智商,这将使客户登录在客户端会话的开放(自动会话关闭问题在10秒内)。

这是我的js:

代码语言:javascript
复制
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: 'myroom@support.myroom/' + 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;
}

请告诉我我做错了什么?我试过并搜索过,但我仍然无法解决。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-20 07:55:19

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

代码语言:javascript
复制
connection.addHandler(onOwnMessage, null, 'iq', 'set', null, null)

onMessage和on_presence处理程序已经足够了,可以像下面的代码那样:

代码语言:javascript
复制
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;
}

下面是一个函数发送消息和更改存在状态的示例:

代码语言:javascript
复制
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); 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33561931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档