0
votes

I use Strophe.Connection to create and initialize a Strophe.Connection object and I start the connection process with 'connect' function but it return ATTACHED status instead of CONNECTED as expected in the book.

This code come from the Jack Moffitt 'Professional XMPP programming with JavaScript and JQuery" but it doesn't work for me :-(

I've passed the all day on it and I'm getting big headache. Any help would be appreciate. Thank you,

Here is the code :

$(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');
        } 
    });

In order to understand what is going on I've modified the code as this (with the connect status):

$(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');

It show me two status : CONNECTING then ATTACHED. Why I can not have CONNECTED status as expected in the book ???

3
There is problem in your code, your last else if block is terminated so it would always execute alert ('status ATTACHED');biplav

3 Answers

0
votes

More information is needed to diagnose this. You might try running in Firefox or Chrome and viewing the network panel in the developer tools to see what error is actually occurring.

I imagine this is just a cross-domain request issue.

0
votes

I go for "metajack"'s suggestion. I have never used chrome network panel, but in firefox, I find firebug quite handy. After you installed firebug, all you need to do is to "enable all panels". Under "Net" tab you have all the requests you send and receive. Under "Net", "All" tab is basically the place where firebug shows all the network activities. Now some where at the bottom of your list you must be able to see your http-bind requests (on my laptop I have BOSH setup on Apache and the Proxy is set as "http-bind"; the exact address depends on your setting). Expand your http-bind node, and select "Post" tab. There you will be able to see your HTTP Post request. Now, if you click on "Response" it shows you the response you have got from your XMPP server. Here is a tutorial by fire bug creator himself.

0
votes

I manage to get Strophe working with my Openfire server by adding a new rule in .htaccess that will redirect the request to http-bind.

RewriteRule http-bind/ http://www.mydomain.info:7070/http-bind/ [P]

The JS code:

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

As a note, the cross-domain policy if applied even if you try to access a resource what is on same domain but on different port.

Hope this helps.

Edit: My .htaccess looks like:

Options +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteRule http-bind/ http://www.domain.info:7070/http-bind/ [P]