2015-02-17 95 views
0

我有這個Android應用程序,我一直試圖設置一個XMPP MultiUserChat。ASmack MultiUserChat(MUC)無法收聽消息

只要連接到XMPP,登錄和創建一個MUC(以及加入一個已經創建的)一切都會好起來的。

我似乎沒有正確聽取發送的消息,我不知道爲什麼。

我已經使用JavaScript(帶有波什的StropheJS)的XMPP工作過幾次,所以我對它的協議有所瞭解。

正如我已經試過就是BEING提出什麼在以下職位(只後,我發現這件事後):

How to properly listen for MultiUserChat in Smack?

所以,我決定問你一些方向,因爲我認爲這可能只是一個我一直想念的設置。因此,爲了簡單起見,我將提供我的整個序列。

我創建了一個服務,所以我的XMPP連接可以遍及整個應用程序。由於我需要在我的應用登錄後立即連接到XMPP,並且MUC室僅在其他活動中進一步使用。那麼,你得到的照片。所以,這裏是代碼:

(對不起,如果這是一個有點太長了,我的意思並不是要討厭,我要的是提供是必要的,以解決它的一切)

//everything is alright here. 
//it's connecting correctly to XMPP 
public void connect() 
{ 

    Thread t = new Thread(new Runnable() { 

     @Override 
     public void run() 
     { 
      boolean flConnected = false; 
      System.setProperty("smack.debugEnabled", "true"); 
      SmackConfiguration.setDefaultPacketReplyTimeout(10000); 


      ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration(Host, Port); 
      ConnectionConfiguration.setSecurityMode(org.jivesoftware.smack.ConnectionConfiguration.SecurityMode.disabled); 

      ConnectionConfiguration.setDebuggerEnabled(true); 
      ConnectionConfiguration.setSendPresence(true); 

      connection = new XMPPTCPConnection(ConnectionConfiguration); 

      try 
      { 
       connection.connect(); 
       login(); 
      } 
      //there are many exceptions being treated, I 
      //suppressed them here for simplicity's sake 
      catch (Exception e) 
      { 
       Log.e(TAG, "connection exception: " + e.getMessage()); 
      } 


     } 
    }); 
    t.start(); 
} 

public void login() 
{ 
    try 
    { 
     connection.login(this.User, Pwd); 

     Presence presence = new Presence(Presence.Type.available); 
     connection.sendPacket(presence); 
     this.setLoginAttempts();   

    } 
    catch (Exception e) 
    { 
     Log.e(TAG, "connection exception: "+e.getMessage()); 
    } 
} 

因此,當用戶應該有一定chatgroup的管理員,我創建組經過組名(ID)

public void createGroup(String strRoom) 
{ 
    try 
    { 
     muc = new MultiUserChat(connection, strRoom+"@"+this.GroupChat); 
     muc.create(this.User); 

     Form form = muc.getConfigurationForm(); 
     Form submitForm = form.createAnswerForm(); 



     for (Iterator fields = form.getFields().iterator(); fields.hasNext();) { 
      FormField field = (FormField) fields.next(); 
      if (!FormField.TYPE_HIDDEN.equals(field.getType()) 
        && field.getVariable() != null) { 
        submitForm.setDefaultAnswer(field.getVariable()); 
      } 
     } 

     List<String> owners = new ArrayList<>(); 
     owners.add(this.User + "@"+this.Host); 

     muc.sendConfigurationForm(submitForm);       

     //here, it's another FAILED attempt of putting the 
     //listener to an async class. Didn't work at all!! 
     //just kept it here so you can see my attempts! 
     //new MessageRunner().execute(connection);   

    } 
    catch (Exception ex) 
    { 
     Log.e(TAG, " exception :"+ex.getMessage()); 
    } 
} 

而當它只是一個參與者,它加入組:

public void joinGroup(String strRoom) 
{ 
    try 
    { 
     DiscussionHistory history = new DiscussionHistory(); 
     history.setMaxStanzas(50); 
     muc = new MultiUserChat(connection, strRoom+"@"+this.GroupChat); 
     muc.join(strRoom, this.Pwd, history, connection.getPacketReplyTimeout()); 

     this.addMessageListener(muc); 

     this.listen2Group(); 
    } 
    catch (Exception ex) 
    { 
     Log.e(TAG, "Exception :"+ex.getMessage()); 
    } 
} 

所以,我有3種方法,我試圖讓用戶正確地聽取消息。

其中一個正在登錄後設置:setListener(connection);

另外兩個用戶之後被稱爲加入GROUPCHAT:

addMessageListener(MUC); 和 listen2Group();

我要離開他們都在這裏,所以你能看到多遠我已經:

private void addMessageListener(MultiUserChat muc) 
{ 
    //it's never coming here! never ever!!! 
    if(null != muc){ 
     muc.addMessageListener(new PacketListener() { 
      @Override 
      public void processPacket(Packet packet) { 
       Log.i("processPacket", "receiving message"); 
      } 
     }); 
    } 
} 

private void listen2Group() 
{ 

    //also, NEVER EVER getting here at any time!! 
    PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat); 


    connection.addPacketListener(new PacketListener() { 
     @Override 
     public void processPacket(Packet packet) throws NotConnectedException 
     { 
      Message message = (Message) packet; 
      if (message.getBody() != null) 
      { 

       //message should be treated here, 
       //but I suppressed it as well. 
      } 
     } 
    }, filter); 
} 

public void setListener(XMPPConnection cnx) 
{ 
    this.connection = cnx; 
    if (connection != null) { 

     PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat); 
     connection.addPacketListener(new PacketListener() { 
      @Override 
      public void processPacket(Packet packet) { 
       Message message = (Message) packet; 
       if (message.getBody() != null) 
       {       
        //message will be treated here. 
       } 
      } 
     }, filter); 
    } 

} 

這裏就是我如何發送消息:已經

public void sendMessage(String msgText) 
{ 
    try 
    { 

     Message msg = new Message(); 
     msg.setBody(msgText); 
     msg.setType(Message.Type.groupchat); 


     muc.sendMessage(msg); 

     //I tried sending with this one as well, nope! 
     //connection.sendPacket(msg); 
    } 
    catch(Exception ex) 
    { 
     Log.e(TAG, " exception :"+ex.getMessage()); 
    } 
} 

所以,也許你看到我做錯了。但我一直在努力工作好幾天。什麼都沒有通過!

當我檢查我的服務器(openfire)時,所有用戶都被設置爲在線,並且正在正確登錄到MUC中,當發送消息時,沒有任何反應!

我把斷點放在我給你看的所有聽衆身上,但他們從不開火,不管我做什麼。該消息被髮送,但它永遠不會到達任何地方。

任何想法?

回答

0

以備將來參考(如咂嘴在其目前的版本是不是所有記錄的方式)

我發現了什麼是錯的聽衆!

事實證明,我沒有設置聊天組的目的地。

由於它本質上是一個GroupChat,我假定它不需要將接收器設置爲消息。需要

一切解決這個問題,在我的方法的sendMessage,一個額外的行:

msg.setTo(this.room + 「@」 + my_service_address);

http://xmpp.org/extensions/xep-0045.html

在XEP-0045它指出了羣聊它必須有一個目標地址,才能正常工作。 「

」在多用戶聊天室內發送的消息是一種特殊類型的「羣聊」,發往房間本身(room @ service),然後反映給所有的居住者。「

相關問題