404 (Invalid SID) in Strophe when using attach ()

I've searched google and some similar questions about Stack Overflow. But I am not getting any solution.

Basically, I want to keep the connection across web pages in my application. I use cookies for this. I store the SID and RID in cookies and use them to reconnect the session.

Below is my code.

this.openConnection = function(){
        if (chatObj.connection != null) return;


        var jid = chatObj.userName + '@' + chatObj.openfireDomainURL; // Bare JID
        var sid = Cookies.get(chatObj.userName + '_sid'); // Session Identifier
        var rid = Cookies.get(chatObj.userName + '_rid'); // Request Identifier

        var connection = new Strophe.Connection(chatObj.xmppHttpBindURL);
        chatObj.connection = connection;

        if(chatObj.isEmpty(sid) && chatObj.isEmpty(rid)){
            connection.connect(jid, chatObj.password, chatObj.onConnection);
        } else {
            // parseInt(rid,10) + 1
            var rid = parseInt(rid) + parseInt(1);
            connection.attach(jid, sid, rid, chatObj.onConnection);
        }
    };

    this.onConnection = function(status) {
        if (status === Strophe.Status.CONNECTED) {
            chatObj.log("CONNECTED ");
            //Cookies.set(chatObj.userName + '_sid', chatObj.connection.sid);
            //Cookies.set(chatObj.userName + '_rid', chatObj.connection.rid);
            jQuery(document).trigger('connected');
        } else if (status === Strophe.Status.DISCONNECTED) {
            jQuery(document).trigger('disconnected');
            chatObj.log("DISCONNECTED ");
        } else if (status === Strophe.Status.CONNFAIL) {
            chatObj.log("CONNFAIL ");
        } else if (status === Strophe.Status.AUTHENTICATING) {
            chatObj.log("AUTHENTICATING ");
        } else if (status === Strophe.Status.AUTHFAIL) {
            chatObj.log("AUTHFAIL ");
        } else if (status === Strophe.Status.ERROR) {
            chatObj.log("ERROR ");
        } else if (status === Strophe.Status.ATTACHED){
            chatObj.log("ATTACHED ");
            jQuery(document).trigger('connected');
        } else if (status === Strophe.Status.CONNFAIL){
            chatObj.log("CONNFAIL ");
        }
    };


$(window).unload(function() {
    console.log("widows unload ");
    if( chatObj.connection != null ){
        console.log("set cookies ");
        Cookies.set(chatObj.userName + '_sid', chatObj.connection.sid);
        Cookies.set(chatObj.userName + '_rid', chatObj.connection.rid);
    } else {
//        Cookies.remove(chatObj.userName + '_sid');
//        Cookies.remove(chatObj.userName + '_rid');
    }
});

      

404 Invalid SID value in Strophe when using attach ()

I tried to use Windows unload but it didn't work. I still get the same error.

+3


source to share


2 answers


Instead of using a method, attach()

there is also a function restore

combined with an option keepalive

:

var connection = new Strophe.Connection(chatObj.xmppHttpBindURL, {'keepalive': true});

      

...

try {
    connection.restore(jid, onConnect);
} catch(e) {
    if (e.name !== "StropheSessionError") { throw(e); }
}

      

See: http://strophe.im/strophejs/doc/1.2.14/files/strophe-js.html#Strophe.Connection.restore

and



https://github.com/strophe/strophejs/blob/master/examples/restore.js


For some strange reason sid

and rid

properties are undefined, so you can get them like this:

  var sid = chatObj.connection._proto.sid;
  var rid = chatObj.connection._proto.rid;

      

Another way is to get them directly from every XML message sent to the connection by overriding xmlOutput :

  connection.xmlOutput = function (e) {
      rid = $(e).attr('rid');
      sid = $(e).attr('sid');
  };

      

+4


source


I also faced the same issue when connecting to the XMPP server. *

I used Strophe version 1.2.2.



*

After using the current version, this issue has been fixed for me.

0


source







All Articles