我用Strophe.Connection创建和初始化一个Strophe.Connection对象,我开始与“连接”功能的连接过程,但它返回附有状态,而不是连按预期在书中。
此代码来自于杰克·莫菲特职业XMPP编程使用JavaScript和JQuery”,但它并没有为我工作:-(
我已经通过了就可以了一整天,我越来越头疼。任何帮助将升值。谢谢,
下面是代码:
$(document).bind('connect', function (ev, data) {
var conn = new Strophe.Connection(
"http://localhost:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
为了理解正在发生的事情我已经修改了代码,因为这(与所述连接状态):
$(document).bind('connect', function (ev, data) {
var conn = new Strophe.Connection(
"http://localhost:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
} else if (status === Strophe.Status.ERROR) {
alert ('status ERROR');
} else if (status === Strophe.Status.CONNECTING) {
alert ('status CONNECTING');
} else if (status === Strophe.Status.CONNFAIL) {
alert ('status CONNFAIL');
} 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');
它告诉我两个状态:连接然后贴。为什么我不能已连接状态如预期在书???
需要更多的信息来诊断这一点。您可以尝试在Firefox或Chrome运行和查看网络面板的开发工具,看看有什么错误实际发生。
我想这只是一个跨域请求的问题。
我去“metajack”的建议。我从来没有使用镀铬网络面板,但在Firefox中,我发现萤火虫很方便。您安装萤火虫后,所有你需要做的就是要“使所有面板”。在“网络”选项卡,你有你发送和接收的请求。在“网”,“全部”选项卡基本上是哪里萤火显示所有网络活动的地方。现在有些凡在列表的底部,你必须能够看到你的HTTP绑定请求(在我的笔记本电脑,我有阿帕奇BOSH设置和代理服务器设置为“HTTP绑定”的确切地址取决于你的设置) 。展开你的HTTP绑定节点,然后选择“发布”选项卡。在那里,你将能够看到HTTP发布您的请求。现在,如果你点击“响应”会显示您从您的XMPP服务器得到的回应。 Here是火的bug创作者自己的教程。
我设法通过增加在.htaccess新规则将请求重定向到HTTP绑定得到的strophe与我的Openfire服务器的工作。
RewriteRule http-bind/ http://www.mydomain.info:7070/http-bind/ [P]
JS代码:
var DEV = {};
var jq = jQuery;
DEV.APP = {};
var BOSH_SERVICE = 'http://www.mydomain.info/http-bind/';
var connection = null;
function log(msg)
{
jq('body').append('<div></div>').append(document.createTextNode(msg));
}
function rawInput(data)
{
log('RECV: ' + data);
}
function rawOutput(data)
{
log('SENT: ' + data);
}
function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
} else if (status == Strophe.Status.CONNECTED) {
log('Strophe is connected.');
// connection.disconnect();
}
}
jq(document).ready(function () {
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
connection.connect("username", "password", onConnect);
});
作为一个说明,应用了跨域策略,如果即使您尝试访问的资源什么是对同一域名,但不同的端口上。
希望这可以帮助。
编辑:我的.htaccess的样子:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule http-bind/ http://www.domain.info:7070/http-bind/ [P]