0

我使用 Strophe.Connection 来创建和初始化一个 Strophe.Connection 对象,并使用“connect”函数启动连接过程,但它返回 ATTACHED 状态而不是书中预期的 CONNECTED 状态。

这段代码来自 Jack Moffitt 的“Professional XMPP programming with JavaScript and 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');

它向我显示了两个状态:CONNECTING 然后 ATTACHED。为什么我不能像书中预期的那样拥有 CONNECTED 状态???

4

3 回答 3

0

需要更多信息来诊断。您可以尝试在 Firefox 或 Chrome 中运行并查看开发人员工具中的网络面板,以查看实际发生的错误。

我想这只是一个跨域请求问题。

于 2012-02-26T16:00:03.953 回答
0

我去“metajack”的建议。我从来没有使用过 chrome 网络面板,但在 firefox 中,我发现 firebug 非常方便。安装 firebug 后,您需要做的就是“启用所有面板”。在“网络”选项卡下,您拥有发送和接收的所有请求。在“Net”下,“All”选项卡基本上是 firebug 显示所有网络活动的地方。现在,在列表底部的某些位置,您必须能够看到您的 http-bind 请求(在我的笔记本电脑上,我在 Apache 上设置了 BOSH,并且代理设置为“http-bind”;确切的地址取决于您的设置) . 展开您的 http-bind 节点,然后选择“发布”选项卡。在那里您将能够看到您的 HTTP Post 请求。现在,如果您单击“响应”,它会显示您从 XMPP 服务器获得的响应。

于 2012-04-13T08:52:34.983 回答
0

我设法让 Strophe 与我的 Openfire 服务器一起工作,方法是在 .htaccess 中添加一条新规则,将请求重定向到 http-bind。

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]
于 2012-02-27T18:40:18.553 回答