Javascript 404使用attach()时Strophe中的SID值无效
我试图用搜索引擎找到我问题的答案,但没能找到 我在backboen项目中使用了Javascript 404使用attach()时Strophe中的SID值无效,javascript,xmpp,strophe,Javascript,Xmpp,Strophe,我试图用搜索引擎找到我问题的答案,但没能找到 我在backboen项目中使用了strophe.muc.js,以使其成为实时网站。现在我正在尝试附加一个会话,这样,如果重新加载一个页面,就不会创建新的连接 function Chat(){ var self = this; this.connection = false; this.jid = false; this.BOSH_SERVICE = 'http://183.155.10.55:7070/http-bind/';
strophe.muc.js
,以使其成为实时网站。现在我正在尝试附加一个会话,这样,如果重新加载一个页面,就不会创建新的连接
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();
};
功能聊天(){
var self=这个;
这个连接=false;
this.jid=false;
这是波什http://183.155.10.55:7070/http-绑定/';
this.init=函数(){
self.connection=新的选通连接(该服务);
self.connection.xmlInput=this.log;
self.connection.xmlOutput=this.log;
if(isObjectNull(sessionStorage.getItem('rid'))&&isObjectNull(sessionStorage.getItem('sid'))&&isObjectNull(sessionStorage.getItem('jid')){
连接(
"",
"",
self.events.onConnect);
}否则{
连接(
sessionStorage.getItem('jid'),
sessionStorage.getItem('sid'),
sessionStorage.getItem('rid'),
self.events.onConnect);
}
};
this.out=函数(消息){
$('#log').append('
').append(
消息
);
};
this.log=函数(消息){
控制台日志(消息);
};
此项。事件={
“onConnect”:功能(状态){
if(状态==选通状态连接){
}else if(状态==Strophe.status.CONNFAIL){
}else if(状态==选通状态断开){
}else if(状态==选通状态断开){
}else if(状态==Strophe.status.CONNECTED | |状态==Strophe.status.ATTACHED){
self.connection.addHandler(函数(msg){
返回self.events.onMessage(msg);
},null,'消息',null,null,null);
self.connection.send($pres().tree());
self.groupchat.join();
}
},
“onMessage”:函数(msg){
if(jQuery(msg).attr(“type”)=“chat”){
}
返回true;
}
};
this.sendMessage=函数(收件人,消息){
var reply=$msg({to:recipient,键入:“chat”})
.c.(“主体”)
.t(信息);
console.log(“发送消息:+消息”);
self.connection.send(reply.tree());
};
this.groupchat={
“_chatRoomId”:WebConfig.chatRoomId,
“_chatRoomNick”:函数(){
var randomGUID=generateGuid();
返回随机GUID;
},
“join”:函数(){
sessionStorage.setItem(“rid”,self.connection.rid);
setItem(“sid”,self.connection.sid);
sessionStorage.setItem(“jid”,self.connection.jid);
试一试{
self.connection.muc.join(
这是我的聊天室,
这个._chatRoomNick(),
这个.incomingMessageHandler,
此.groupPresenceHandler,
无效的
);
}捕获(e){
控制台错误(e);
}
},
“消息”:功能(消息){
试一试{
self.connection.muc.groupchat(
这是我的聊天室,
味精,
无效的
);
}捕获(e){
$错误(e);
}
},
“incomingMessageHandler”:函数(消息){
日志(“消息句柄”);
if(jQuery(msg).attr(“type”)=“chat”){
self.out(“”+jQuery(msg).attr(“from”)+:“+jQuery(msg).find(“body:first”).text());
}
},
“groupPresenceHandler”:函数(状态){
控制台日志(“状态句柄”);
控制台日志(存在);
}
};
this.init();
};
在使用attach()之前,刷新页面时除了连接丢失外,一切都很好。但是在我添加了attach()之后,我得到了一个错误'POST 404(无效的SID值)。如果有人遇到了与我相同的问题,我可以通过将代码调整到稍高一点(更改设置
rid
、SID
和jid
的位置,从join()
到$(窗口)。unload()
无需增加rid
$(window).unload(function() {
sessionStorage.setItem("rid",self.connection.rid);
sessionStorage.setItem("sid",self.connection.sid);
sessionStorage.setItem("jid",self.connection.jid);
});