我正在尝试附加会话,以便在重新加载页面时,不会创建新的连接:
//在Document Ready上,检查是否设置了jid.sid和rid cookies:如果是: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)加载页面, - 成功创建Cookie - 跳转到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”错误。 任何解决方案?