我正在开发通过BOSH服务与xmpp用户进行通信的通用库。我使用Strophe.js如何在成功连接时获得正确的Strophe.js回调
我不明白为什么在类BoshCommunicator方法onConnect我有一个Strophe对象,但不是预期的BoshCommunicator。
当我CONSOLE.LOG(这个)我得到对象{服务= “HTTP:// myboshim/HTTP绑定”,JID = “aivaras37 @ myboshim/29785298701381498988721337”,摆脱= 4092107607,更...}
此外,在建立连接前我设置监听器属性BoshCommunicator类。当我console.log(this.listener)我得到未定义,但如果我connect()方法console.log(this.listener)我得到预期的转储侦听器对象。你能解释一下如何在BoshCommunicator.prototype.onConnect()中初始化BoshCommunicator吗?同时请解释为什么我放弃了我的对象,取而代之的是Strophe。
这里是一个特殊的代码,我使用:
function BoshCommunicator(boshUri, host)
{
this.connection = null;
this.boshUri = null;
this.host = null;
this.listener = null;
this.setBoshUri(boshUri);
this.setHost(host);
}
BoshCommunicator.prototype.setBoshUri = function(boshUri)
{
this.boshUri = boshUri;
};
BoshCommunicator.prototype.setHost = function(host)
{
this.host = host;
};
BoshCommunicator.prototype.setListener = function(listener)
{
this.listener = listener;
this.listener.setListenHost(this.host);
};
BoshCommunicator.prototype.connect = function(jid, jpass)
{
this.connection = new Strophe.Connection(this.boshUri);
this.connection.connect(jid, jpass, this.onConnect);
};
BoshCommunicator.prototype.onConnect = function(status)
{
if (status === Strophe.Status.CONNECTED) {
console.log("Send me messages!");
console.log(this); // dumps strophe object. Paste from console: Object { service="http://myboshim/http-bind", jid="[email protected]/29785298701381498988721337", rid=4092107607, more...}
console.log(this.listener); // undefined - how come?
this.connection.addHandler(this.listener.onReceivedMessage, null, 'message', null, null, null);
this.connection.send($pres().tree());
}
};
function MessageListener()
{
this.host = null;
}
MessageListener.prototype.setListenHost = function(host)
{
this.host = host;
};
function ReceivedMessageNotify()
{
MessageListener.call(this);
}
ReceivedMessageNotify.prototype = new MessageListener();
ReceivedMessageNotify.prototype.constructor = ReceivedMessageNotify;
ReceivedMessageNotify.prototype.onReceivedMessage = function(message)
{
console.log(message);
var elems = message.getElementsByTagName('body');
var body = elems[0];
if (Strophe.getText(body).length > 0) {
this.sendNewMessageNotify(message.getAttribute('from'), Strophe.getText(body));
}
return true;
};
ReceivedMessageNotify.prototype.sendNewMessageNotify = function(sender, messageBody)
{
alert("New message: " + sender + " wrote: " + messageBody);
};
$(document).ready(function() {
var communicator = new BoshCommunicator('http://myboshim/http-bind', 'myboshim');
communicator.setListener(new ReceivedMessageNotify());
communicator.connect('jabberId', 'accPasswd');
});
这可能需要移到stackoverflow。无论如何,你更有可能得到很好的答案和评论。这个网站更关注软件开发的一般概念,而stackoverflow处理代码中的真实技术问题和类似的东西。 – Panzercrisis
Panzercrisis,我该如何迁移我的问题? – Aivaras