Strophe不使用javascript调用iq-addHandler
Strophe does not invoke iq addHandler using javascript
我正在开发一个使用Strophejs连接XMPP openfire的应用程序。问题是presence发送正确,但iq处理程序从未被调用。我在浏览器中检查了控制台,但没有发现错误。
我想发送presence和发送iq,这将使客户端登录openfire的客户端会话(10秒内自动关闭会话)。
这是我的js:
function onConnect(status) {
debugger;
if (status == Strophe.Status.CONNECTING) {
alert('Strophe is connecting.');
log('Strophe is connecting.');
} else if (status === Strophe.Status.AUTHENTICATING) {
alert ('status AUTHENTICATING');
} else if (status === Strophe.Status.AUTHFAIL) {
alert ('status AUTHFAIL');
} else if (status === Strophe.Status.ATTACHED) {
alert ('status ATTACHED');
} else if (status == Strophe.Status.CONNFAIL) {
alert('Strophe failed to connect.');
log('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
alert('Strophe is disconnecting.');
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
alert('Strophe is disconnected.');
log('Strophe is disconnected.');
reConnectTimer = setInterval(reConnect, 3000);
} else if (status == Strophe.Status.CONNECTED) {
connection.addHandler(onOwnMessage, null, 'iq', 'set', null, null);
connection.addHandler(onMessage, null, 'message', null, null, null);
connection.addHandler(on_presence, null, 'presence', null, null, null);
connection.send($pres().tree());
var pres = $pres({ to: 'myroom@support.myroom/' + Math.random() });
connection.send(pres);
alert('Strophe is connected.');
log('Strophe is connected.');
clearInterval(reConnect);
//connection.disconnect();
}
}
function onOwnMessage(msg) {
debugger;
// console.log(msg);
alert('msg is: ' + msg);
var elems = msg.getElementsByTagName('own-message');
if (elems.length > 0) {
var own = elems[0];
var to = $(msg).attr('to');
var from = $(msg).attr('from');
var iq = $iq({
to: from,
type: 'error',
id: $(msg).attr('id')
}).cnode(own).up().c('error', { type: 'cancel', code: '501' })
.c('feature-not-implemented', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' });
connection.sendIQ(iq);
alert(iq);
}
return true;
}
请告诉我我做错了什么?我试过并在谷歌上搜索过,但还是无法解决。
提前谢谢。
您的代码来自https://gist.github.com/RudyLu/5641350,这是使用Strophe.js连接到facebook聊天的示例代码。我想你会在自己的XMPP服务器上创建一个简单的聊天(例如,使用帖子中标签所述的Openfire)。在这种情况下,您不需要IQ处理程序:
connection.addHandler(onOwnMessage, null, 'iq', 'set', null, null)
onMessage和on_sance处理程序就足够了,可以像以下代码一样:
function onMessage(msg) {
var to = msg.getAttribute('to');
var from = msg.getAttribute('from');
var type = msg.getAttribute('type');
var elems = msg.getElementsByTagName('body');
if (type == "chat" && elems.length > 0) {
var body = elems[0];
console.log('CHAT: I got a message from ' + from + ': ' + Strophe.getText(body));
}
// we must return true to keep the handler alive.
// returning false would remove it after it finishes.
return true;
}
function on_presence (presence){
console.log('onPresence:');
var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc...
var from = $(presence).attr('from'); // the jabber_id of the contact
console.log(' >'+from+' --> '+presence_type);
if (presence_type != 'error'){
if (presence_type === 'unavailable'){
// Mark contact as offline
}else{
var show = $(presence).find("show").text(); // this is what gives away, dnd, etc.
if (show === 'chat' || show === ''){
// Mark contact as online
}else{
// etc...
}
}
}
return true;
}
以下是发送消息和更改存在状态的功能示例:
function sendMessage(msg, to) {
console.log('CHAT: Send a message to ' + to + ': ' + msg);
var m = $msg({to: to, from: connection.jid, type: 'chat'}).c("body").t(msg);
connection.send(m);
}
function setStatus(s) {
console.log('setStatus: '+s);
var status = $pres().c('show').t(s);
connection.send(status);
}
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- node.js:setInterval()正在跳过调用
- 如何在单击复选框后调用控制器方法
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 使用Google Visualization动态调用构造函数
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 调用函数内部的函数
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- Javascript/jQuery中的并行Ajax调用
- Strophe不使用javascript调用iq-addHandler