2015-11-02 94 views
1

我想在xmpp中的組(多用戶)聊天中發送組合事件,我正在使用asmack庫,我已經完成了與一對一聊天功能相同的功能。如何在多用戶聊天/羣聊中發送撰寫/正在輸入(聊天狀態)事件以及在xmpp android中一對一聊天?

我使用下面的代碼:

mMessageEventManager = new MessageEventManager(XMPPConnectApplication.getInstance().getXmppConnection()); 

       mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() { 

        @Override 
        public void offlineNotification(String arg0, String arg1) { 

        } 

        @Override 
        public void displayedNotification(String arg0, String arg1) { 

        } 

        @Override 
        public void deliveredNotification(String arg0, String arg1) { 

        } 

        @Override 
        public void composingNotification(String from, String to) { 
         Log.e("Receiver-composingNotification",from + " is started typing......"+to); 

        } 

        @Override 
        public void cancelledNotification(String from, String to) { 
         Log.e("Receiver-cancelledNotification",from + " is stopped typing......"+to); 

        } 
       }); 

請讓我知道如果你有同樣的想法。

任何幫助將不勝感激。

回答

5

是的,我對此有所瞭解,並在1周前完成了。

enter image description here

我用MessageEventManager管理聊天美國

private MessageEventManager mMessageEventManager; 

添加這種方法聊天狀態下接收監聽

private void chatStateRecognizer(){ 

     Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       mMessageEventManager = new MessageEventManager(mXmppConnection); 

       mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() { 

        @Override 
        public void offlineNotification(String arg0, String arg1) { 

        } 

        @Override 
        public void displayedNotification(String arg0, String arg1) { 

        } 

        @Override 
        public void deliveredNotification(String from, String arg1) { 
        } 

        @Override 
        public void composingNotification(String from, String to) { 
         Log.i("Receiver:Compose state",from + " is started typing......"+to); 
        } 

        @Override 
        public void cancelledNotification(String from, String to) { 
         Log.i("Receiver:Stop state",from + " is stopped typing......"+to); 

        } 
       }); 
      } 
     }); 

     thread.start(); 
    } 

GroupInfoModel.java創建一個型號類別名稱:

public class GroupInfoModel implements Comparable<GroupInfoModel>, Serializable{ 

    private static final long serialVersionUID = 1L; 
    private String memberId = "", memberName = ""; 
    private boolean isAdmin; 
    public String getMemberId() { 
     return memberId; 
    } 
    public void setMemberId(String memberId) { 
     this.memberId = memberId; 
    } 
    public String getMemberName() { 
     return memberName; 
    } 
    public void setMemberName(String memberName) { 
     this.memberName = memberName; 
    } 
    public boolean isAdmin() { 
     return isAdmin; 
    } 
    public void setAdmin(boolean isAdmin) { 
     this.isAdmin = isAdmin; 
    } 
    @Override 
    public int compareTo(GroupInfoModel another) { 
     return getMemberName().compareTo(another.getMemberName()); 
    } 
} 

現在採取的ArrayList的GroupInfoMo del.java類:

private ArrayList<GroupInfoModel> groupDetailsList = new ArrayList<GroupInfoModel>(); 

private boolean isComposingStarted; 

的onCreate()片段活動/

groupDetailsList.clear(); 
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(mXmppConnection); 
DiscoverItems items = discoManager.discoverItems(mRoomId); 
    for (Iterator<Item> it = items.getItems(); it.hasNext();) { 
     DiscoverItems.Item item = (DiscoverItems.Item) it.next(); 
     String occupant = item.getEntityID(); 
     occupant = occupant.split("/")[1]; 
     GroupInfoModel groupInfoModel = new GroupInfoModel(); 
     groupInfoModel.setAdmin(false); 
     groupInfoModel.setMemberId(occupant+"@"+mServiceNameHere); 
     groupInfoModel.setMemberName(occupant); 
     groupDetailsList.add(groupInfoModel); 
    } 

現在在撰寫郵件的EditText添加TextWatcher聊天視圖)scre EN:

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if(s.toString().length()==1&&!isComposingStarted){ 
     isComposingStarted = true; 
     if(chatType.equals("OneToOneChat")){ 
      mMessageEventManager.sendComposingNotification(myJabberId, friendJabberId); 
     }else if(chatType.equals("GroupChat")){ 
      for (int i = 0; i < groupDetailsList.size(); i++) { 
      if(!groupDetailsList.get(i).getMemberId().contains(myJabberId)){ 
       mMessageEventManager.sendComposingNotification(groupDetailsList.get(i).getMemberId(), roomId);  
      } 
      } 
     } 
    }else if(s.toString().length()==0){ 
     isComposingStarted = false; 
     if(chatType.equals("OneToOneChat")){ 
      mMessageEventManager.sendCancelledNotification(myJabberId, friendJabberId); 
     }else if(chatType.equals("GroupChat")){ 
      for (int i = 0; i < groupDetailsList.size(); i++) { 
       if(!groupDetailsList.get(i).getMemberId().contains(myJabberId)){ 
       mMessageEventManager.sendCancelledNotification(groupDetailsList.get(i).getMemberId(), roomId); 
       } 
      } 
     } 
    } 
} 

強烈建議應用類上面的代碼中使用,您可以修改方法爲您的要求。

完成

+0

謝謝。工作正常。 –

+0

很高興爲您效勞。 –

+0

我做了同樣的事情,但它沒有發送或接收任何組合狀態..請幫助我找出解決方案... – Kutbi

1
// send multi user chat typing status 
public static void sendMUCTypingStatus(ChatState state) 
{ 
// check if you are connected to group 
if(multiUserChat != null) 
{ 
    try{ 
     // create packet 
     Message statusPacket = new Message(); 
     // set body to null 
     statusPacket.setBody(null); 
     // set packet type to group chat 
     statusPacket.setType(Message.Type.groupchat); 
     // set subject to null 
     statusPacket.setSubject(null); 
     // set to the group name 
     statusPacket.setTo(multiUserChat.getRoom()); 
     // set from my current jis example : [email protected] 
     statusPacket.setFrom(new MyPrefrence(XmppBase.context).getUsername()); 
     // get the chat state extension and pass our state 
     ChatStateExtension extension = new ChatStateExtension(state); 
     // add the extention to our packet 
     statusPacket.addExtension(extension); 
     // get the connection and send the packet 
     Utils.getConnection().sendStanza(statusPacket); 
    } catch (SmackException.NotConnectedException e) { 
     e.printStackTrace(); 
    } 
} 
} 

用法:

sendMucTypingStatus(ChatState.composing); 

關注此:Quick overview of using

0

隨着RxJavaJake Wharton's RxBinding,這是很簡單的事:

RxTextView.afterTextChangeEvents(editText) 
    .observeOn(Schedulers.io()) 
    .skip(1) 
    .map({ input -> 
     // FIRE ChatState.composing EVENT HERE 
     input // just returning the argument here 
    }) 
    .debounce(2, TimeUnit.SECONDS) 
    .observeOn(Schedulers.io()) 
    .subscribe { 
     // FIRE ChatState.active EVENT HERE 
    } 

請記住,我們將不得不編寫代碼來通過smack stanzaListener捕獲這些事件並相應地在UI上顯示它!

代碼是用Kotlin編寫的,但它非常簡單。