2017-08-08 70 views
3

我搜索了谷歌和堆棧溢出一些類似的問題。但我沒有得到任何解決方案。404(無效的SID值)在Strophe使用附加()

基本上,我想堅持跨應用程序中的網頁連接。爲此,我正在使用Cookie。我將SID和RID存儲在Cookie中,並將它們用於再次附加會話。

以下是我的代碼。

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 while using attach()

我嘗試使用Windows卸載,但它不工作。我仍然得到同樣的錯誤。

回答

4

而不是使用attach()方法也有個restore功能與keepalive選項結合使用:

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

...

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

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

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


由於一些奇怪的原因sidrid性是不確定的,所以您可以通過以下方式進行檢索:

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

另一種方法是直接從發送到連接的每個XML消息進行檢索覆蓋xmlOutput功能:

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

我確實得到了sid和rid值。即使這樣也行不通。 – ashishjmeshram

+0

你的項目中使用了哪個版本的strophe.js? – beaver

+0

我正在使用的strophe版本是1.2.14 – ashishjmeshram