2011-08-24 64 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客戶端獲得您的即時聊天。