简体   繁体   中英

smack presence listener in multi user chat

smack presence listener in multi user chat not getting called. Used Smack Api to login and then added roster.addRosterListener(mRoasterListener); but could not get any success to listen when presence of other user of the chat room changes. I tried following code to get the presence listener to work :

connection.login(loginUser, passwordUser);

MultiUserChatManager manager = 

MultiUserChatManager.getInstanceFor(connection);

muc = manager.getMultiUserChat(roomID + "@" +context.getString(R.string.group_chat_id));

Log.d("Join User: ", "Already Created");

muc.join(Utilities.getUserPhoneNo(context));

muc.addMessageListener(mGroupMessageListener);

Roster roster = Roster.getInstanceFor(connection);//luna

roster.addRosterListener(mRoasterListener);//roasterListener

Log.d("Joined User Phone: ", " " + Utilities.getUserPhoneNo(context));

and this class to listen for presence change...

public class RoasterListener implements RosterListener{
        public RoasterListener(Context context){

        }

        @Override
        public void entriesAdded(Collection<String> collection) {

        }

        @Override
        public void entriesUpdated(Collection<String> collection) {

        }

        @Override
        public void entriesDeleted(Collection<String> collection) {

        }

        @Override
        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
        }
    }

I tried many links available by stackoverflow but could not get any success. Please Help!

For Multi User Chat you don't have to use Roster, because it's normal to meet people you don't have in Roster.

To know who is in a muc, first ask for occupants:

muc.join(user,password);

List<String> occupantsAtJoinTime = muc.getOccupants();

                    for (String occupant : occupantsAtJoinTime)
                    {
                        System.out.println("occupant: "+occupant);
                        //actions
                    }

then, to keep Occupants list updated, register a DefaultParticipantStatusListener to your muc and define that Listner:

muc.addParticipantStatusListener(new CustomParticipantStatusListner());

definied as (there are many methods to implement if you need):

    public class CustomParticipantStatusListner extends DefaultParticipantStatusListener 
    {

        public void joined(String participant) 
        {
            System.out.println(participant + "just joined MUC");
//actions (add occupantsRightNow)
        }

        public void left(String participant)
        {
            System.out.println(participant + " just left MUC");
//actions (remove occupantsRightNow)
        }
    }

All this with smack 4.1.7

It's about the Manage role modifications in Multi User Chat. This example shows how to grant voice to a visitor and listen for the notification events:

// User1 creates a room
  muc = new MultiUserChat(conn1, "myroom@conference.jabber.org");
  muc.create("testbot");

  // User1 (which is the room owner) configures the room as a moderated room
  Form form = muc.getConfigurationForm();
  Form answerForm = form.createAnswerForm();
  answerForm.setAnswer("muc#roomconfig_moderatedroom", "1");
  muc.sendConfigurationForm(answerForm);

  // User2 joins the new room (as a visitor)
  MultiUserChat muc2 = new MultiUserChat(conn2, "myroom@conference.jabber.org");
  muc2.join("testbot2");
  // User2 will listen for his own "voice" notification events
  muc2.addUserStatusListener(new DefaultUserStatusListener() {
      public void voiceGranted() {
          super.voiceGranted();
          ...
      }
      public void voiceRevoked() {
          super.voiceRevoked();
          ...
      }
  });

  // User3 joins the new room (as a visitor)
  MultiUserChat muc3 = new MultiUserChat(conn3, "myroom@conference.jabber.org");
  muc3.join("testbot3");
  // User3 will lister for other occupants "voice" notification events
  muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
      public void voiceGranted(String participant) {
          super.voiceGranted(participant);
          ...
      }

      public void voiceRevoked(String participant) {
          super.voiceRevoked(participant);
          ...
      }
  });

  // The room's owner grants voice to user2
  muc.grantVoice("testbot2"); 

Details can be refered in http://web.mit.edu/svalente/lib/smack_3_0_4/documentation/extensions/muc.html .

Firstly, join a chat room:

public MultiUserChat joinMultiUserChat(String user, String roomsName,
        String password) {
    if (getConnection() == null)
        return null;
    try {
        MultiUserChat muc = new MultiUserChat(getConnection(), roomsName
                + "@conference." + getConnection().getServiceName());
        DiscussionHistory history = new DiscussionHistory();
        history.setMaxChars(0);
        // history.setSince(new Date());
        muc.join(user, password, history,
                SmackConfiguration.getPacketReplyTimeout());
        Log.i("MultiUserChat", "Chat room【"+roomsName+"】joined........");
        return muc;
    } catch (XMPPException e) {
        e.printStackTrace();
        Log.i("MultiUserChat", "Chat room【"+roomsName+"】failed........");
        return null;
    }
}

Then, use MultiChatUser to send Message:

try {
    multiUserChat.sendMessage(message);
} catch (XMPPException e) {
    e.printStackTrace();
}

Add a Listener:

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

public class TaxiMultiListener implements PacketListener {
    @Override
    public void processPacket(Packet packet) {
        Message message = (Message) packet;
        String body = message.getBody();
    }
}

Finally, call the Listener using MultiUserChat:

multiUserChat.addMessageListener(new TaxiMultiListener());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM