我正在开发一个需要包含会话附件的 XMPP Web 应用程序。我正在使用 Openfire XMPP 服务器。我已经成功实现了创建会话的服务器端附件,并且我得到了 sid 并摆脱了会话。不幸的是,我被困在下一部分。
据我所知,在获得 sid 和 rid 后,我需要使用 Strophe.Connection.attach() 将 strphe 附加到现有会话。之后,我发出一个 IQ 请求,该请求从服务器获得正确的响应。但是,我的状态没有任何变化,即应用程序既没有连接也没有附加,因此我无法做任何事情。IQ 请求后,服务器按时响应正常消息,然后在下一个请求中,我收到 403 禁止错误并终止连接。
这是代码:
var BOSH_SERVICE = 'http://localhost/cosys-rtw/';
var connection = null;
var sid = "";
var rid = 0;
function notifyUser(msg)
{
if (msg.getAttribute('from') == "testuser1@ghost/pingstream") {
var elems = msg.getElementsByTagName('body');
var body = elems[0];
$('#notifications').append(Strophe.getText(body));
}
return true;
}
function onConnect(status)
{
console.log("Connect")
console.log(status)
if (status == Strophe.Status.ATTACHED) {
$('#notifications').html('<p class="welcome">Hello! Any new posts will appear below.</p>');
connection.addHandler(notifyUser, null, 'message', null, null, null);
connection.send($pres().tree());
}
}
$(document).ready(function () {
$.ajax({'url':'http://localhost/x',
'method':'get',
'success':function(response){
console.log(response);
console.log(jQuery.parseJSON(response));
resp = jQuery.parseJSON(response);
window.sid = resp.sid;
window.rid = resp.rid;
prepareStrophe();
}});
});
function prepareStrophe(){
console.log(BOSH_SERVICE);
jid = "testuser1@ghost";
connection = new Strophe.Connection(BOSH_SERVICE);
console.log(window.rid);
console.log(window.sid);
connection.attach( jid,window.sid,window.rid,null);
connection.sendIQ( $iq({to:Strophe.getDomainFromJid(jid),type:"get"})
.c('query',{xmlns:'http://jabber.org/protocol/disco#info'}),
function(){
if (status == Strophe.Status.ATTACHED) {
connection.send($pres().tree());}
});}
我收到对 IQ 请求的以下响应:
<body xmlns="http://jabber.org/protocol/httpbind">
<iq id="6173:sendIQ" xmlns="jabber:client" type="result" from="ghost" to="testuser1@ghost/6b8bfe07">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="server" name="Openfire Server" type="im"></identity>
<identity category="pubsub" type="pep"></identity>
<feature var="http://jabber.org/protocol/pubsub#delete-nodes"></feature>
.
.
.
<feature var="http://jabber.org/protocol/pubsub#purge-nodes"></feature>
<feature var="http://jabber.org/protocol/disco#info"></feature>
<feature var="http://jabber.org/protocol/rsm"></feature>
</query>
</iq>
</body>
此响应确实表明会话有效。我对吗?我无法弄清楚这件事,所以现在我将其交给社区。顺便说一句——圣诞快乐