我试图用搜索引擎找到我的问题的答案,但我无法做到。
我在我的backboen项目中使用strophe.muc.js
以使其成为实时网站。现在我正在尝试附加一个Session,这样如果页面重新加载,就不会创建新的连接。
function Chat(){
var self = this;
this.connection = false;
this.jid = false;
this.BOSH_SERVICE = 'http://183.155.10.55:7070/http-bind/';
this.init = function () {
self.connection = new Strophe.Connection(this.BOSH_SERVICE);
self.connection.xmlInput = this.log;
self.connection.xmlOutput = this.log;
if(isObjectNull(sessionStorage.getItem('rid')) && isObjectNull(sessionStorage.getItem('sid')) && isObjectNull(sessionStorage.getItem('jid'))) {
self.connection.connect(
"",
"",
self.events.onConnect);
}else{
self.connection.attach(
sessionStorage.getItem('jid'),
sessionStorage.getItem('sid'),
sessionStorage.getItem('rid'),
self.events.onConnect);
}
};
this.out = function (message) {
$('#log').append('<br />').append(
message
);
};
this.log = function( message ) {
console.log( message );
};
this.events = {
"onConnect": function (status) {
if (status == Strophe.Status.CONNECTING) {
} else if (status == Strophe.Status.CONNFAIL) {
} else if (status == Strophe.Status.DISCONNECTING) {
} else if (status == Strophe.Status.DISCONNECTED) {
} else if (status == Strophe.Status.CONNECTED || status == Strophe.Status.ATTACHED) {
self.connection.addHandler(function (msg) {
return self.events.onMessage( msg );
}, null, 'message', null, null, null);
self.connection.send($pres().tree());
self.groupchat.join();
}
},
"onMessage": function( msg ) {
if(jQuery(msg).attr("type") == "chat") {
}
return true;
}
};
this.sendMessage = function( recipient, message ) {
var reply = $msg({to: recipient, type: "chat"})
.c("body")
.t(message);
console.log("SENDING MESSAGE: " + message);
self.connection.send(reply.tree());
};
this.groupchat = {
"_chatRoomId": WebConfig.ChatRoomID,
"_chatRoomNick": function(){
var randomGUID = generateGuid();
return randomGUID;
},
"join":function () {
sessionStorage.setItem("rid",self.connection.rid);
sessionStorage.setItem("sid",self.connection.sid);
sessionStorage.setItem("jid",self.connection.jid);
try {
self.connection.muc.join(
this._chatRoomId,
this._chatRoomNick(),
this.incomingMessageHandler,
this.groupPresenceHandler,
null
);
} catch (e) {
console.error(e);
}
},
"message":function (msg) {
try {
self.connection.muc.groupchat(
this._chatRoomId,
msg,
null
);
} catch (e) {
$.error(e);
}
},
"incomingMessageHandler": function ( msg ) {
console.log("MESSAGE HANDLE");
if(jQuery(msg).attr("type") == "chat") {
self.out( "<strong>" + jQuery(msg).attr("from") + ": </strong>" + jQuery(msg).find("body:first").text());
}
},
"groupPresenceHandler": function ( presence ) {
console.log("PRESENCE HANDLE");
console.log(presence);
}
};
this.init();
};
在使用attach()之前,在刷新页面时连接丢失之后,一切都运行良好。但是在我添加了attach()后,我收到错误`POST http://183.155.10.55:7070/http-bind/ 404(无效的SID值。)
答案 0 :(得分:2)
如果有人遇到与我相同的问题,我已经通过稍微调整我的代码来完成它(更改地点设置rid
,sid
和{{1从jid
到join()
。
$(window).unload()
无需增加$(window).unload(function() {
sessionStorage.setItem("rid",self.connection.rid);
sessionStorage.setItem("sid",self.connection.sid);
sessionStorage.setItem("jid",self.connection.jid);
});
。