4

我正在尝试附加一个会话,以便如果重新加载页面,则不会创建新连接:

// 在 Document Ready 时,检查是否设置了 jid.sid 和 rid cookie:如果是:conn.attach。其他连接:

$(document).ready(function()
{
    var cookie_data = {
        jid: $.cookie('xmpp_jid'),
        sid: $.cookie('xmpp_sid'),
        rid: $.cookie('xmpp_rid')
    };

    if(cookie_data.jid && cookie_data.sid && cookie_data.rid)
    {
         console.log("Cookies exists");
         var conn = new Strophe.Connection('/xmpp-httpbind');
         conn.attach(cookie_data.jid, cookie_data.sid, cookie_data.rid,function(status){
             console.log(status);
         }); 
     }else{
         connect();
      }
});


function connect()
{
   var conn = new Strophe.Connection('/xmpp-httpbind');

   conn.connect('xxxxx','yyyyy',function(status)
   {
      if(status === Strophe.Status.CONNECTED)
      {
        var iq = $iq({type:'get'}).c('query',{xmlns:'jabber:iq:roster'});
        Chat.connection.sendIQ(iq,Chat.on_roster);
        Chat.connection.addHandler(Chat.handle_message,null, "message", "chat");
        Chat.connection.send($pres());

        $.cookie('xmpp_jid', conn.jid, {path: '/'});
        $.cookie('xmpp_sid', conn.sid, {path: '/'});
        $.cookie('xmpp_rid', conn.rid+1, {path: '/'});  
    }
    });
    Chat.connection = conn; 
}





var Chat = {
    connection: null
}

所以:

1)加载页面,-cookies创建成功-跳转到connect()--连接成功创建

2) 再次加载页面 - 存在 Cookie 值。conn.attach回调返回以下内容

Status-Code 8:         ATTACHED
then INVALID SID ERROR (404)
then Status-Code 7:    DISCONNECTING
then Status-Code 6:    DISCONNECTED

问题是“INVALID SID”错误。有什么解决办法吗?

4

0 回答 0