XMPP Pubsub问题使用smack订阅和发布

时间:2014-10-11 10:22:02

标签: xmpp smack

我正在尝试将Pubsub模块用于我的应用程序,现在我正在尝试运行一些发布/订阅测试,到目前为止没有结果。

在服务器端,我使用安装了pubsub的ejabberd 2.1.11(或者至少看起来像是这样)

mod_pubsub_odbc:     [{access_createnode, pubsub_createnode},
{pep_sendlast_offline, false},
{last_item_cache, false},
{plugins, ["flat", "hometree", "pep"]}]

在客户端,我已经能够创建节点,但我无法订阅它们。这是我的代码

XMPPConnection mycon = new XMPPTCPConnection(connectionConfiguration);

mycon.connect();
mycon.login("phantom_notifier", "phantom_notifier");

PubSubManager mgr = new PubSubManager(mycon);
LeafNode leaf = mgr.getNode("testNode2");

leaf.addItemEventListener(new ItemEventListener()
{
    @Override
    public void handlePublishedItems(ItemPublishEvent items) {
        //TODO
    }
});
leaf.subscribe(mycon.getUser());

运行此代码后,我总是得到以下异常

java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream> from line 1, parser stopped on END_TAG seen ...</x></query></iq>... @1:3734
at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:3035)
at org.xmlpull.mxp1.MXParser.more(MXParser.java:3046)
at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)
at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:279)
at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:47)
at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:81)

org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:191)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:175)
at org.jivesoftware.smackx.pubsub.PubSubManager.sendPubsubPacket(PubSubManager.java:303)
at org.jivesoftware.smackx.pubsub.Node.sendPubsubPacket(Node.java:443)
at org.jivesoftware.smackx.pubsub.Node.subscribe(Node.java:248)
at main.DBChangesReceiver.main(DBChangesReceiver.java:115)

无论我订阅哪个节点......

另一方面,每当我尝试发布消息时,我都没有收到任何错误但是检查日志我发现我的消息不对。我用来发布消息的代码如下

leaf.publish(new PayloadItem<SimplePayload>("test" + System.currentTimeMillis(),
new SimplePayload("book", "pubsub:test:book",
"test book")));

在调试器中,我上次发送和接收的消息如下:

已发送消息

<iq id="BT97r-10" to="pubsub.myserver" type="set">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
   <publish node="testNode2">
    <item id="test1413022577642">test book</item>
  </publish>
</pubsub>

收到消息

<iq id="BT97r-10" to="phantom_notifier@myserver/Smack" from="pubsub.myserver" type="error">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
  <publish xmlns="http://jabber.org/protocol/pubsub">
    <item>test book</item>
  </publish>
 </pubsub>
 <error type="modify">
   <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
   <invalid-payload xmlns="http://jabber.org/protocol/pubsub#errors"/>
  </error>
</iq>

现在我对这个无效的有效载荷一无所知,在互联网上查看文档我看不出有什么大的差别。

编辑

在@Flow建议之后,发送到服务器的包是:

<iq id="XIaTx-11" to="pubsub.myserver" type="set">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
  <publish node="testNode2">
   <item id="test1413099855673">
     <book xmlns="pubsub:test:book">text book</book>
   </item>
  </publish>
 </pubsub>
</iq>

服务器不会发送响应,因为它似乎存在一些严重错误(与我尝试订阅时相同的注释):

oct 12, 2014 9:44:15 AM org.jivesoftware.smack.XMPPConnection callConnectionClosedOnErrorListener
Connection closed with error
java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream> from line 1, parser stopped on END_TAG seen ...</x></query></iq>... @1:3625
at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:3035)
at org.xmlpull.mxp1.MXParser.more(MXParser.java:3046)
at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)
at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:279)
at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:47)
at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:81)

这里我有ejabberd错误日志(希望它很有用)

=ERROR REPORT==== 2014-10-12 09:45:38 ===
** State machine <0.4880.3> terminating
** Last message in was {route,
                    {jid,[],"pubsub.myserver",[],[],
                     "pubsub.myserver",[]},
                    {jid,"phantom_notifier","myserver","Smack",
                     "phantom_notifier","myserver","Smack"},
                    {xmlelement,"iq",
                     [{"type","error"},
                      {"from","pubsub.myserver"},
                      {"id","XIaTx-11"}],
                     [{xmlelement,"pubsub",
                       [{"xmlns","http://jabber.org/protocol/pubsub"}],
                       [{xmlelement,"publish",
                         [{"node","testNode2"}],
                         [{xmlelement,"item",
                           [{"id","test1413099855673"}],
                           [{xmlelement,"book",
                             [{"xmlns","pubsub:test:book"}],
                             [{xmlcdata,<<"text book">>}]}]}]}]},
                      {aborted,no_transaction}]}}


** When State == session_established
**      Data  == {state,
                 {socket_state,gen_tcp,#Port<0.218148>,<0.4879.3>},
                 ejabberd_socket,#Ref<0.0.12.23022>,false,"2115632325",
                 {sasl_state,"jabber","myserver",[],
                     #Fun<ejabberd_c2s.1.30334249>,
                     #Fun<ejabberd_c2s.2.32721014>,
                     #Fun<ejabberd_c2s.3.7052687>,cyrsasl_digest,
                     {state,5,"3654209317","phantom_notifier",[],
                         #Fun<ejabberd_c2s.1.30334249>,
                         #Fun<ejabberd_c2s.3.7052687>,
                         ejabberd_auth_internal,"myserver"}},
                 c2s,c2s_shaper,false,true,false,false,
                 [verify_none,{certfile,"/etc/ejabberd/ejabberd.pem"}],
                 true,
                 {jid,"phantom_notifier","myserver","Smack",
                     "phantom_notifier","myserver","Smack"},
                 "phantom_notifier","myserver5","Smack",
                 {{1413,99937,977476},<0.4880.3>},
                 {pres_t,1},
                 {pres_f,1},
                 {pres_a,1},
                 {pres_i,0},
                 {xmlelement,"presence",
                     [{"id","XIaTx-9"}],
                     [{xmlelement,"status",[],
                          [{xmlcdata,<<"Phantom on the phone">>}]},
                      {xmlelement,"priority",[],[{xmlcdata,<<"42">>}]}]},
                 undefined,
                 {{2014,10,12},{7,45,37}},
                 false,
                 {userlist,none,[],false},
                 c2s,ejabberd_auth_internal,
                 {{84,125,131,61},56127},
                 []}
** Reason for termination =
** {{badxml,
    {xmlelement,"iq",
        [{"from","pubsub.myserver"},
         {"to","phantom_notifier@myserver/Smack"},
         {"type","error"},
         {"id","XIaTx-11"}],
        [{xmlelement,"pubsub",
             [{"xmlns","http://jabber.org/protocol/pubsub"}],
             [{xmlelement,"publish",
                  [{"node","testNode2"}],
                  [{xmlelement,"item",
                       [{"id","test1413099855673"}],
                       [{xmlelement,"book",
                            [{"xmlns","pubsub:test:book"}],
                            [{xmlcdata,<<"text book">>}]}]}]}]},
         {aborted,no_transaction}]},
    {{case_clause,{aborted,no_transaction}},
     [{xml,element_to_string_nocatch,1},
      {xml,'-element_to_string_nocatch/1-lc$^0/1-0-',1},
      {xml,'-element_to_string_nocatch/1-lc$^0/1-0-',1},
      {xml,element_to_string_nocatch,1},
      {xml,element_to_string,1},
      {xml,element_to_binary,1},
      {ejabberd_c2s,send_element,2},
      {ejabberd_c2s,handle_info,3}]}},
[{xml,element_to_string,1},
 {xml,element_to_binary,1},
 {ejabberd_c2s,send_element,2},
 {ejabberd_c2s,handle_info,3},
 {p1_fsm,handle_msg,10},
 {proc_lib,init_p_do_apply,3}]}

我们可以看到的错误与客户端中给出的错误有关,这是一个构造错误的xml元素。

1 个答案:

答案 0 :(得分:3)

我认为问题是item只有文本而没有封闭的XML元素。对于Smack的SimplePayload课来说,这是一个不幸的设计问题。基本上你想要替换

"test book"

"<book xmlns='pubsub:test:book'>text book</book>"

构造SimplePayload