【发布时间】:2014-08-02 16:03:59
【问题描述】:
我已经为 JavaScript 实现了一个 XMPP 客户端(使用 strophe.js),为此我指的是 Jack Moffitt 的Professional XMPP with JavaScript and jQuery一书。
这本书对使用“strophe.js”的 XMPP JavaScript 客户端的工作进行了很好的解释。
使用我的代码,XMPP Web 客户端需要 6 秒才能连接到 XMPP 服务器(使用 BOSH 的 ejabberd),这对我的应用程序来说是不可取的。
有人能解释一下为什么会这样吗?
我的 XMPP 客户端代码如下:
var Arthur = {
connection: null,
jid:"20147001@localhost",
password: "XXXX2014",
handle_message: function (message) {
if ($(message).attr('from')) {
if($(message).attr('from').match(/^abcd007@localhost/)){
console.log("inside message received");
var body = $(message).find('body').contents();
var span = $("<span></span>");
body.each(function () {
if (document.importNode) {
$(document.importNode(this, true)).appendTo(span);
console.log(span);
console.log(span.text());
}
});
notiReceived(span.text());
console.log("afte notiReceived executed");
}
if($(message).attr('from').match(/^xyz2014@localhost/)){
console.log("inside message received");
var body1 = $(message).find('body').contents();
var span1 = $("<span></span>");
body1.each(function () {
if (document.importNode) {
$(document.importNode(this, true)).appendTo(span1);
//console.log(span.find('text'));
console.log(span1);
console.log(span1.text());
}
});
notiReceived(span1.text());
console.log("afte notiReceived executed");
}
}
return true;
}
};
function sendPushNotification(to,operation,request_id,message){
if(to=="citizen"){
var myObject = new Object();
myObject.FROM="Executive";
myObject.FUNCTION=operation;
myObject.MESSAGE = message;
myObject.REQUESTID= request_id;
console.log("inside citizen::"+myObject);
var myString = JSON.stringify(myObject);
$(this).val('');
var msg = $msg({to: 'abcd007@localhost', type: 'chat'})
.c('body').t(myString);
console.log("inside keypress data is::"+msg);
Arthur.connection.send(msg);
}
if(to=="technician"){
var myObject1 = new Object();
myObject1.FROM="Executive";
myObject1.FUNCTION=operation;
myObject1.MESSAGE = "Check Request Status";
myObject1.REQUESTID= request_id;
console.log("inside technician:"+myObject1);
var myString1 = JSON.stringify(myObject1);
$(this).val('');
var msg1 = $msg({to: 'xyz2014@localhost', type: 'chat'})
.c('body').t(myString1);
console.log("after msg send to technician"+msg1);
Arthur.connection.send(msg1);
}
};
function connected() {
console.log("inside connected");
Arthur.connection.addHandler(Arthur.handle_message,
null, "message", "chat");
console.log("inside connected");
Arthur.connection.send($pres());
};
function disconnected() {
console.log("disconnected");
Arthur.connection = null;
};
function disconnect(){
Arthur.connection.disconnect();
};
function connectXMPP() {
var conn = new Strophe.Connection(
hosturl.URL+":5280/http-bind");
console.log("inside connection");
conn.connect(Arthur.jid, Arthur.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
connected();
console.log("connected");
} else if (status === Strophe.Status.DISCONNECTED) {
disconnected();
}
});
Arthur.connection = conn;
};
【问题讨论】:
-
您是说
conn.connect()回调需要6 秒才能被触发?尝试使用您的浏览器网络调试器来查看它是否只是来自 ejabberd 的缓慢响应。 -
是的,conn.connect() 被触发后需要 6 秒,正如你所说,当涉及到 bosh 时,ejabberd 的响应速度很慢,我试图从我的 android 应用程序连接到 ejabberd 服务器使用 asmak ,在我的移动数据网络(2g)上花费的时间不超过 1 秒!!!!!!!!!!!!
-
我想在这里分享的一个观察结果“conn.connect()”正在同步处理,因此在一段时间内(比如 6 秒),浏览器会挂起!!!
标签: javascript jquery xmpp strophe