2016-09-20 128 views
1

嘿,我正在使用XMPP Smack Library開發聊天應用程序。最近我正在進行羣聊,在發送羣組消息時,一些消息將被丟棄,因此接收者不會從發送方接收消息。它會給我400個不好的要求。Android Smack MUC 400錯誤請求發送羣組聊天消息

它有時正在工作。有時不工作

在這裏我發現這種類型的消息在400壞請求。

<?xml version="1.0" encoding="UTF-8"?> 
<message to="[email protected]/Android" id="nXlV6-1144" type="error" from="[email protected]/[email protected]"> 
    <received xmlns="urn:xmpp:receipts" id="nXlV6-1142" /> 
    <error code="400" type="modify"> 
     <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /> 
    </error> 
</message> 

如果成功發送消息,它會給這種消息。

<?xml version="1.0" encoding="UTF-8"?> 
<message to="[email protected]/Android" id="nXlV6-1411" type="groupchat" from="[email protected]/[email protected]"> 
    <body>eyu4u4</body> 
    <chatDetail xmlns="jabber:x:oob"> 
     <UID>156</UID> 
     <time>04:20 PM</time> 
     <user_icon>24_group_icon.jpg</user_icon> 
     <SentTime>1474368652960</SentTime> 
     <USERName>vasudev89</USERName> 
     <user_name>cryan</user_name> 
     <message>eyu4u4</message> 
     <type>group</type> 
     <phone_number>24</phone_number> 
    </chatDetail> 
    <request xmlns="urn:xmpp:receipts" /> 
</message> 

我該如何持續發送消息?任何想法? 預先感謝您。

這裏是我的代碼發送睦消息:

public boolean sendGroupMessage(Message message, String strGroupID) { 

      DeliveryReceiptRequest.addTo(message); 
      try { 
       Log.i(TAG, "sendGroupMessage"); 
       //Log.i("JOIN MUC","To join group chat: " + groupChat.getClassId()); 

       // Get the MultiUserChatManager 
       MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(AbstractXMPPConnection); 

       // Create a MultiUserChat using an XMPPConnection for a room 
       MultiUserChat muc = manager.getMultiUserChat(strGroupID + AppWSConstants.XMPP_JID_GROUP_CHAT_SUFFIX); 
       muc.sendMessage(message); 
       return true; 
      } catch (NotConnectedException e) { 
       e.printStackTrace(); 
      } 
      return false; 
     } 

回答

0

我的「看到並發表」中MUC,並回答後面具有相同的分組ID時,還在測試這個問題面臨的工作,但我認爲,在你的情況你應該將你的xmpp連接移動到後臺服務,並且在設備啓動時連接到xmpp後,連接到數據庫中的所有muc。這樣你將永遠被連接到組。

原因:根據我,當其他用戶沒有連接到muc,並且您發送消息或當您回覆具有相同數據包id的組時。

注:我使用multiuserchat.sendmessage發送羣消息和chat.sendmessage要發送消息給單個用戶

SMACK 4.1

**更新**

我通過創建新數據包來修復它,而不是修改我正在接收的數據包

這裏是數據包

Message msgg = new Message(); 
msgg.setBody(message.getPacketID()); 
msgg.setSubject(MessageModel.CHAT_STATUS_SEEN + ""); 
XmppBase.sendMUCChatMsg(msgg); 

在你的情況下,先嚐試簡單的數據包。如果一切運作良好,然後逐個增加分機上,看看你的錯誤,謝謝

+0

HI我認爲這是同一個包的問題,​​但我仍確認的多SNIP代碼更新用戶發送消息...我也有連接問題,它被斷開很多次 –

+0

我有斷開連接問題,但是當我將它移動到後臺服務時,它被修復了。 還有一件事,如果您使用的是openfire服務器,啓用存檔聊天功能,並且在發送消息「不良請求」後,看看用戶是否斷開連接 也應該使用yourmuc.sendmessage發送組消息,而不是chat.sendmessage。 注意:發送消息時不要指定** TO **或** FROM ** –

+0

我已經更新了我上面的發送代碼。 –

0

我有一個XmppBase類在這裏我把我所有的XMPP代碼

所有聽衆在單獨的文件夾

連接存儲在靜態變量中,我使用

Utils。的getConnection()

// this function m calling from background service and everywhere if not connectect to xmpp 
public static XMPPConnection CreateXmppConnection() { 
    if (Utils.getConnection() == null) { 
     try { 
      Boolean isConnected = new XmppAsync(mUsername, mPassword,context).execute().get(); 
      if (isConnected && Utils.getConnection() != null) { 
       RegisterConnListeners(Utils.getConnection()); 
       updateMyProfileImg(); 

       // connect to all groups 
       DBAdapter adapter = new DBAdapter(context); 
       adapter.openForRead(); 
       List<UserDetail> groups = new ArrayList<>(); 
       adapter.addAllGroups(groups); 
       adapter.addPastChatGroups(groups); 
       adapter.close(); 

       for(UserDetail g : groups) 
       { 
        CreateXmppMUCSession(g.getGroupTemp()); 
       } 

       return Utils.getConnection(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } else 
     return Utils.getConnection(); 
} 

// get muc chat manager 
public static MultiUserChatManager getMucManager() { 

    if(mucManager != null) 
     return mucManager; 

    if (Utils.getConnection() != null) { 
     return MultiUserChatManager.getInstanceFor(Utils.getConnection()); 
    } else { 
     if (CreateXmppConnection() != null) 
      return MultiUserChatManager.getInstanceFor(Utils.getConnection()); 
     else { 
      Log.v("error", "Some Error Occured"); 
      Toast.makeText(context, "Cant Connect to Xmpp", Toast.LENGTH_SHORT).show(); 
      return null; 
     } 

    } 

} 

// create muc session and m passing group name - call when you open chat page 
public static void CreateXmppMUCSession(String gName) 
{ 
    RegisterGroupChatListeners(gName); 
} 

// connect to muc if not already connected 
public static void RegisterGroupChatListeners(String groupName) 
{ 
    try { 
     mStateManager = getChatStateManager(); 
     multiUserChat = getMUC(groupName); 
     // if(multiUserChat != null) { 
     multiUserChat.addMessageListener(new MyMUCMessageListener()); 
     try { 
      if (!multiUserChat.isJoined()) { 
       DiscussionHistory discussionHistory = new DiscussionHistory(); 
       discussionHistory.setMaxStanzas(0); 
       multiUserChat.join(new MyPrefrence(context).getUsername().split("@")[0], "123", 
         discussionHistory, SmackConfiguration.getDefaultPacketReplyTimeout()); 
      } 
     } catch (SmackException.NoResponseException e) { 
      e.printStackTrace(); 
     } catch (XMPPException.XMPPErrorException e) { 
      e.printStackTrace(); 
     } catch (SmackException.NotConnectedException e) { 
      e.printStackTrace(); 
     } 
     // } 
    } 
    catch (Exception ex) 
    { 
     // 
    } 

} 

// get muc 
public static MultiUserChat getMUC(String groupName) 
{ 
    // Log.v("nick",multiUserChat.getNickname() + " , g = " + groupName); 
    // if(multiUserChat != null && multiUserChat.getRoom().contains(groupName)) 
    // { 
    //  return multiUserChat; 
    // } 

    if (Utils.getConnection() != null) { 
     MultiUserChatManager chatManager = getMucManager(); 
     if (chatManager != null) { 
      return chatManager.getMultiUserChat(groupName); 
     } else { 
      Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show(); 
      return null; 
     } 
    } else { 
     if (CreateXmppConnection() != null) { 
      MultiUserChatManager chatManager = getMucManager(); 
      if (chatManager != null) { 
       return chatManager.getMultiUserChat(groupName); 
      } else { 
       Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show(); 
       return null; 
      } 
     } 
     else { 
      Toast.makeText(context, "Cannot create Chat", Toast.LENGTH_SHORT).show(); 
      return null; 
     } 

    } 
} 

,每當我要發送的消息,我只是把這個

public static Boolean sendMUCChatMsg(Message msg) 
{ 
    if(multiUserChat != null) 
     try { 
      multiUserChat.sendMessage(msg); 

      return true; 
     } catch (SmackException.NotConnectedException e) { 
      e.printStackTrace(); 
     } 
    return false; 
} 

很抱歉,如果它看起來clumpsy,如果我錯過了有什麼功能讓我知道,但這是工作代碼我使用

+0

你能告訴我如何邀請朋友嗎?和GroupChat在某些時候從openfire刪除。 可以請你幫忙。 – LearnPainLess

+0

是的,爲了邀請朋友檢查我對這個問題的另一個答案,我現在將添加。 爲groupchat,我做了一個後臺服務的xmpp連接,所以請按照下列步驟: –

+0

抱歉無法輸入評論,我會爲此發佈新的答案 –

1

試試這個,

我修改你的最後一個函數

static MultiUserChat multiUserChat; 
// call this function when you open the chat window 
private void CreateGroupConnection(String strGroupID) { 

// Get the MultiUserChatManager 
      MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(AbstractXMPPConnection); 

      // Create a MultiUserChat using an XMPPConnection for a room 
      MultiUserChat multiUserChat = manager.getMultiUserChat(strGroupID + AppWSConstants.XMPP_JID_GROUP_CHAT_SUFFIX); 

return multiUserChat; 
} 

// whenever sending message from chat call this 

publilc static void sendMucMessage(Message message){ 
if(multiUserChat != null) 
multiUserChat.sendMessage(message); 
} 
2

@LearnPainLess,請按照下列步驟來解決羣聊問題

- 當創建組,保存組的JID在數據庫中,像「[email protected]{domain}.com」

- 創建後臺任務創建XMPP連接(這樣一來就會始終連接)中XMPP

-after記錄,從數據庫中獲取的組名,並連接到他們

此外,在Openfire的羣聊>羣聊設置>編輯圖標>默認房間設置>選中「讓路持久性」

此外,在其他設置>從不踢空閒用戶

+0

好吧,讓我試試這個.. :) 感謝您的回覆。 – LearnPainLess

+0

謝謝你的作品。但我的郵件沒有收到任何用戶。 – LearnPainLess

+0

也去openfire>服務器設置選項卡>離線消息(左菜單)>檢查存儲,存儲或刪除(並設置每個用戶的最大大小) 這樣做之後,您還設置數據包偵聽器或節偵聽器接收包 –

相關問題