2011-08-24 377 views
1

我有我自己配置​​的OpenFire服务器。我创建了两个用户user1user2,我试图通过Smack API在这些用户之间发送/接收消息。不能通过Smack API与openfire服务器发送/重新发送消息

我的Smack API开启调试模式,可以看到我的虚拟应用程序发送和接收消息,但MessageListener不起作用,它永远不会调用processMessage(Chat chat, Message message)方法,也是我无法看到的米兰达IM客户端的消息user2。

代码:

public class Main { 
    public static void main(String[] args) throws XMPPException, InterruptedException { 
     sendJabberMessage(); 

     while (true) { 
      Thread.sleep(50); 
     } 
    } 

    public static void sendJabberMessage() throws XMPPException { 
    XMPPConnection.DEBUG_ENABLED = true; 
    ConnectionConfiguration config = new ConnectionConfiguration("ejab.net", 5222, "ejab.net"); 
    SASLAuthentication.supportSASLMechanism("PLAIN", 0); 

    config.setCompressionEnabled(true); 
    config.setSASLAuthenticationEnabled(true); 

    XMPPConnection connection = new XMPPConnection(config); 
    connection.connect(); 
    connection.login("user1", "password"); 
    Chat chat = connection.getChatManager().createChat("[email protected]", new MessageListener() { 
     public void processMessage(Chat chat, Message message) { 
      System.out.println("Received message: " + message); 
     } 
    }); 
} 

这里是我的调试日志:

发送日志

<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"> 
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/> 
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"> 
<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">b3BlcmF0b3IyQGVqYWIubmV0AG9wZXJhdG9yMkBlamFiLm5ldAA4ZGRYUGdQYTU0MlQ=</auth> 
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"> 
<iq id="4KApN-0" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>Smack</resource></bind></iq> 
<iq id="4KApN-1" type="set"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq> 
<compress xmlns='http://jabber.org/protocol/compress'> 
<method>zlib</method></compress> 
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"> 
<iq id="4KApN-2" type="get"><query xmlns="jabber:iq:roster"></query></iq> 
<presence id="4KApN-3"></presence> 
<message id="4KApN-4" to="[email protected]" from="[email protected]/Smack" type="chat"><body>Hello world</body><thread>T0T5u0</thread></message> 
<iq id="mir_148" to="[email protected]/Miranda" type="result"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="Smack" type="pc"/><feature var="http://jabber.org/protocol/xhtml-im"/><feature var="http://jabber.org/protocol/muc"/><feature var="http://jabber.org/protocol/commands"/></query></iq> 

接受的日志

<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>JIVE-SHAREDSECRET</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms></stream:features> 
<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/> 
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>JIVE-SHAREDSECRET</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features> 
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/> 
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features> 
<iq type="result" id="4KApN-0" to="ejab.net/4942adbf"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>[email protected]/Smack</jid></bind></iq> 
<iq type="result" id="4KApN-1" to="[email protected]/Smack"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq> 
<compressed xmlns='http://jabber.org/protocol/compress'/> 
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features> 
<iq type="result" id="4KApN-2" to="[email protected]/Smack"><query xmlns="jabber:iq:roster"><item jid="[email protected]" name="user1" subscription="both"/></query></iq> 
<presence from="[email protected]/Miranda" to="[email protected]/Smack"><priority>0</priority><c xmlns="http://jabber.org/protocol/caps" node="http://miranda-im.org/caps" ver="0.9.28.0" ext="pmuc-v1 mood activity mir_notes"/><x xmlns="vcard-temp:x:update"><photo/></x><status>Yep, I'm here.</status></presence> 
<iq type="get" to="[email protected]/Smack" id="mir_147" from="[email protected]/Miranda"><query xmlns="jabber:iq:version"/></iq> 
<iq type="get" to="[email protected]/Smack" id="mir_148" from="[email protected]/Miranda"><query xmlns="http://jabber.org/protocol/disco#info"/></iq> 
<message type="chat" to="[email protected]/Smack" id="mir_151" from="[email protected]/Miranda"><body>TEST</body></message> 

有人可以帮助我,说为什么我的客户不打印在控制台上消减消息,以及为什么user2不接收消息;日志不包含任何错误。

+0

此外:客户端已成功连接到服务器,user1显示在Miranda客户端在线显示 – Vitaly

+0

[得到同样的问题,但解决此问题后,在第一次尝试收到我的消息](http://stackoverflow.com/questions/28023749 /发送和接收的消息贯通XMPP协议上机器人/ 28040155#28040155) – learner

回答

0

这两个用户实际上都需要在接收消息之前互相打开聊天。你应该打开user1和user2之间的聊天,注册一个MessageListener,它将监听user2发送的消息(你创建的实际上是侦听user2发送的消息,而你的消息日志是说你从user2发送消息给user1),然后以相反的方式做同样的事情。最后发送一条消息,你就可以处理它。

而且,只是一个意见,请在您的标题更具体的API与Openfire的

0

我有点被你说什么糊涂效果很好,什么是你的代码,你表现出对您调试日志。

你的代码(和你的文章)说你是从Smack客户端以user1身份登录的,但是你的日志显示你是以user2发送和从user1接收的。我认为这只是报告中的一个简单错误。

真正的问题似乎是要发送消息给

[email protected] 

和从

[email protected]/Miranda 

接收消息这些是2个不同的用户。您似乎正在发送给不同的用户,而不是您收到的。因此Miranda客户端收不到信息的原因是因为Smack客户端没有将它发送给正确的JID,当然这与你听众中没有收到任何东西的原因是一样的。

另一方面,要接收传入的消息,您可以创建一个ChatManagerListener,然后您将从Miranda客户端获得您的即时聊天。