我正在使用Strophe JS& Openfire做简单的pubsub。
设置:
使用案例
两个具有两个不同用户会话的浏览器窗口(xmpp会话)
火狐(用户1)---->出版商
铬(用户2)---->订户
代码:
//XConn represents Strophe Connection
//creating PUBSUB NODE (Firefox)
XConn.pubsub.createNode(
XConn.jid,
'pubsub.localhost',
'PUBSUB_NODE',
{},
function (){
}
);
//Adding subscriber to created node (Chrome)
XConn.pubsub.subscribe(
XConn.jid,
'pubsub.localhost',
'PUBSUB_NODE',
[],
function(msg){ console.log(msg);},
function(sub){ }
);
//publishing item to node (Firefox)
XConn.pubsub.publish(XConn.jid,'pubsub.localhost','PUBSUB_NODE',[ '<item><book xmlns="pubsub:test:book"><title>Book1</title></book></item>']);
XConn.pubsub.publish(XConn.jid,'pubsub.localhost','PUBSUB_NODE',[ '<item><book xmlns="pubsub:test:book"><title>Book2</title></book></item>']);
我期望发布者(firefox)发布的消息都应该打印在chrome(订阅者)控制台上。但它只打印第一个,看起来只有一次调用Subscribe-callback。
答案 0 :(得分:4)
我发现了订阅回拨的问题。 如果有兴趣获得更多触发器,Strophe JS插件(pubsub)需要从回调中返回'true'。
所以改变回调以恢复真实有效。
代码更改为以上代码段:
//Adding subscriber to created node (Chrome)
XConn.pubsub.subscribe(
XConn.jid,
'pubsub.localhost',
'PUBSUB_NODE',
[],
function(msg){ console.log(msg); return true; },
function(sub){ return true;}
);