2015-11-06 54 views
2

我正在使用Strophejs與XMPP openfire連接的應用程序。問題是存在發送正確,但iq處理程序不會被調用。我在瀏覽器中查看了控制檯,但沒有發現錯誤。Strophe不使用javascript調用iq addHandler

我想發送狀態併發送iq,這將使客戶端登錄到openfire的客戶端會話(10秒內自動關閉會話問題)。

這裏是我的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: '[email protected]/' + 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; 
} 

請告訴我,我究竟做錯了什麼?我已經嘗試並Google搜索,但我仍然無法解決。

在此先感謝。

回答

2

您的代碼是從https://gist.github.com/RudyLu/5641350,這是使用Strophe.js連接到Facebook聊天的示例代碼。 我想你會根據你自己的XMPP服務器創建一個簡單的聊天(例如,使用Openfire,如你的帖子中的標籤所述)。 在這種情況下,你不需要智商的處理程序:

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

在onMessage和on_presence處理程序是夠了,也能像下面的代碼:

這裏是發送消息功能的示例並更改存在狀態:

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); 
} 
+0

感謝@beaver的指導。我現在正在學習XMPP,Strophe;這就是爲什麼我面臨這樣的問題... 你可以請指導我關於'智商'在strophe。? 我很困惑 –