0
votes

I use strophe.js to build a simple IM(web).

I have 2 pages:index.html(for login) and myChat.html(main chat view).

And when I login to openfire server by jid and password in index.html

connection = new Strophe.Connection(BOSH_SERVICE);     
connection.connect($("#inputName").val()+"@openfireserver", $("#inputPassword").val(), onConnect);  

Then I save jid,sid,rid in COOKIE in callback 'onConnect', and go to myChat.html

if (status == Strophe.Status.CONNECTED) {  
    console.log("success");  
    connected = true; 
    $.cookie('jid', connection.jid);
    $.cookie('sid', connection._proto.sid);
    $.cookie('rid', connection._proto.rid);
    location.href='myChat.html';
}  

In myChat.html, I use connection.attach() to restore the BOSH session.

    var jid=$.cookie('jid');
    var sid=$.cookie('sid');
    var rid=$.cookie('rid');
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.attach(jid,sid,parseInt(rid,10)+1,onConnectAttach);

At first, in callback 'onConnectAttach', the status==Strophe.Status.ATTACHED, but wait 1-3 mins, status will turn into Strophe.Status.CONNFAIL and Strophe.Status.CONNECTED!

If I use connection.connect() to re-connect the server, it will keep session all the time.

So I can not understand where I make the mistake? Why the status will change after I use attach()?

1

1 Answers

1
votes

Got it.

connection.attach(jid,sid,parseInt(rid,10)+1,onConnectAttach);

It's wrong, because I look through some questions about "attach" function and I find someone say that 'rid' must be added one.

But, by my test, I don't need to do that.just:

connection.attach(jid,sid,rid,onConnectAttach);