2015-11-02 108 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编写的,但它非常简单。