我在openfire中开了一个永久房间,然后用strophe进行登录能够成功!在openfire的房间成员里也能看到登录的用户列表
但是我用conn.send(msg)发送消息时,onMessage没有响应,也没有给我返回错误信息!我不知道应该在那里查看错误信息,不知道是我的消息格式有问题还是其他什么原因
下面是我发送消息的代码,不知道有没有错 var BOSH_SERVICE = "/http-bind";
var mConn=null;
var roomName = "[email protected];
var roomJID = "";
var userJID = ""; function MUCMessageHandler(msg){
console.log("已取得分组对话信息:", Strophe.serialize(msg));
var sFrom = $(msg).attr("from");
var sType = $(msg).attr("type");
var sBody = $(msg).attr("body");
/*console.log("FROM:"+sFrom);
console.log("TYPE:"+sType);
console.log("BODY:"+sBody);*/
roomJID = sFrom; $("#btnLogin").text("登录成功,再次点击退出");
} function eneterRoom(jid){//用户JID:user@mypc/nickname
console.log('onEnterRoom:'+roomName+"/"+jid);
var pres = $pres({
/*id:'v'+new Date().getTime(),
to:jid+"/"+ ($("#userID").val().split("@")[0])*/
from:jid,
to:roomName+"/"+(jid.split("@")[0])
}).c(
'x',{
xmlns : 'http://jabber.org/protocol/muc'
}
).tree();
//console.log(pres);
mConn.send(pres); /*$pres({
from: jid,
to: ROOM_JID + "/" + jid.substring(0,jid.indexOf("@"))
}).c('x',{xmlns: 'http://jabber.org/protocol/muc'}).tree()); */ } function onMessage(msg) {
var sFrom = $(msg).attr("from");
var sType = $(msg).attr("type");
var sBody = $(msg).find("> body");
if (sType=='chat'){
//私聊,单对单
console.log("o2o");
}else if(sType=='groupchat'){
//群聊,加入房间后聊天
console.log("TYPE:"+sBody);
if (sBody){
$("#msgbox").html("进入"+"<br/>"+$("#msgbox").html());
}else{
$("#msgbox").html("错误"+"<br/>"+$("#msgbox").html());
} }else{
console.log("chat"+msg);
} } function onConnect(status) {
if (status==Strophe.Status.CONNECTING){
console.log("正在连接XMPP服务器!")
}else if(status==Strophe.Status.CONNECTED){
console.log("连接XMPP服务器成功!")
$("#btnLogin").text("正在进入房间...");
userJID = mConn.jid;
$.cookie('jid', mConn.jid);
$.cookie('sid', mConn.sid);
$.cookie('rid', mConn.rid);
mConn.addHandler(onMessage,null,"message",null,null,null);
mConn.send($pres().tree()); //进入群聊房间
eneterRoom(mConn.jid);
}else if (status==Strophe.Status.CONNFAIL){
console.log("XMPP服务器连接失败!");
}else if (status==Strophe.Status.CONNTIMEOUT){
console.log("XMPP服务器连接超时!");
}else if(status==Strophe.Status.DISCONNECTED){
console.log("成功断开与服务器的连接!");
$("#btnLogin").text("进入房间");
}else if (status==Strophe.Status.DISCONNECTING){
console.log("正在断开与服务器的连接!");
}
} $(document).ready(function (e) {
mConn = new Strophe.Connection(BOSH_SERVICE);
mConn.addHandler(MUCMessageHandler,null,"message","groupchat");
$("#btnLogin").click(function (e) {
if($(this).text()=="进入房间"){
$(this).text("正在进入,请稍候......"); var uname = $("#userID").val();
var upass = $("#userPW").val();
console.log("UName:"+uname);
console.log("UPass:"+upass);
mConn.connect(uname,upass,onConnect);
}else{ mConn.disconnect();
}
}); $("#btnSend").click(function (e) {
var txt = $("#sendText").val(); if (txt.length==""){
alert("请输入要说的话!");
return;
}
if(mConn){
console.log(userJID+":"+userJID.split("/")[0]);
var msg = $msg({
to:roomJID,
from:userJID.split("/")[0],
type:'groupchat'
}).c('body',null,txt);
//console.log(msg.tree());
mConn.send(msg.tree());
}else{
console.log("服务器链接丢失!");
}
})
});
但是我用conn.send(msg)发送消息时,onMessage没有响应,也没有给我返回错误信息!我不知道应该在那里查看错误信息,不知道是我的消息格式有问题还是其他什么原因
下面是我发送消息的代码,不知道有没有错 var BOSH_SERVICE = "/http-bind";
var mConn=null;
var roomName = "[email protected];
var roomJID = "";
var userJID = ""; function MUCMessageHandler(msg){
console.log("已取得分组对话信息:", Strophe.serialize(msg));
var sFrom = $(msg).attr("from");
var sType = $(msg).attr("type");
var sBody = $(msg).attr("body");
/*console.log("FROM:"+sFrom);
console.log("TYPE:"+sType);
console.log("BODY:"+sBody);*/
roomJID = sFrom; $("#btnLogin").text("登录成功,再次点击退出");
} function eneterRoom(jid){//用户JID:user@mypc/nickname
console.log('onEnterRoom:'+roomName+"/"+jid);
var pres = $pres({
/*id:'v'+new Date().getTime(),
to:jid+"/"+ ($("#userID").val().split("@")[0])*/
from:jid,
to:roomName+"/"+(jid.split("@")[0])
}).c(
'x',{
xmlns : 'http://jabber.org/protocol/muc'
}
).tree();
//console.log(pres);
mConn.send(pres); /*$pres({
from: jid,
to: ROOM_JID + "/" + jid.substring(0,jid.indexOf("@"))
}).c('x',{xmlns: 'http://jabber.org/protocol/muc'}).tree()); */ } function onMessage(msg) {
var sFrom = $(msg).attr("from");
var sType = $(msg).attr("type");
var sBody = $(msg).find("> body");
if (sType=='chat'){
//私聊,单对单
console.log("o2o");
}else if(sType=='groupchat'){
//群聊,加入房间后聊天
console.log("TYPE:"+sBody);
if (sBody){
$("#msgbox").html("进入"+"<br/>"+$("#msgbox").html());
}else{
$("#msgbox").html("错误"+"<br/>"+$("#msgbox").html());
} }else{
console.log("chat"+msg);
} } function onConnect(status) {
if (status==Strophe.Status.CONNECTING){
console.log("正在连接XMPP服务器!")
}else if(status==Strophe.Status.CONNECTED){
console.log("连接XMPP服务器成功!")
$("#btnLogin").text("正在进入房间...");
userJID = mConn.jid;
$.cookie('jid', mConn.jid);
$.cookie('sid', mConn.sid);
$.cookie('rid', mConn.rid);
mConn.addHandler(onMessage,null,"message",null,null,null);
mConn.send($pres().tree()); //进入群聊房间
eneterRoom(mConn.jid);
}else if (status==Strophe.Status.CONNFAIL){
console.log("XMPP服务器连接失败!");
}else if (status==Strophe.Status.CONNTIMEOUT){
console.log("XMPP服务器连接超时!");
}else if(status==Strophe.Status.DISCONNECTED){
console.log("成功断开与服务器的连接!");
$("#btnLogin").text("进入房间");
}else if (status==Strophe.Status.DISCONNECTING){
console.log("正在断开与服务器的连接!");
}
} $(document).ready(function (e) {
mConn = new Strophe.Connection(BOSH_SERVICE);
mConn.addHandler(MUCMessageHandler,null,"message","groupchat");
$("#btnLogin").click(function (e) {
if($(this).text()=="进入房间"){
$(this).text("正在进入,请稍候......"); var uname = $("#userID").val();
var upass = $("#userPW").val();
console.log("UName:"+uname);
console.log("UPass:"+upass);
mConn.connect(uname,upass,onConnect);
}else{ mConn.disconnect();
}
}); $("#btnSend").click(function (e) {
var txt = $("#sendText").val(); if (txt.length==""){
alert("请输入要说的话!");
return;
}
if(mConn){
console.log(userJID+":"+userJID.split("/")[0]);
var msg = $msg({
to:roomJID,
from:userJID.split("/")[0],
type:'groupchat'
}).c('body',null,txt);
//console.log(msg.tree());
mConn.send(msg.tree());
}else{
console.log("服务器链接丢失!");
}
})
});
<message to="[email protected]/ah38gmsf6j" from="user@mypc" type="groupchat" xmlns="jabber:client"><body>111</body></message>