Skip to content Skip to sidebar Skip to footer

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 l

Solution 1:

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(newCustomParticipantStatusListner());

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

publicclassCustomParticipantStatusListnerextendsDefaultParticipantStatusListener 
    {

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

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

All this with smack 4.1.7

Solution 2:

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 = newMultiUserChat(conn1, "myroom@conference.jabber.org");
  muc.create("testbot");

  // User1 (which is the room owner) configures the room as a moderated roomForm 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 = newMultiUserChat(conn2, "myroom@conference.jabber.org");
  muc2.join("testbot2");
  // User2 will listen for his own "voice" notification events
  muc2.addUserStatusListener(newDefaultUserStatusListener() {
      publicvoidvoiceGranted() {
          super.voiceGranted();
          ...
      }
      publicvoidvoiceRevoked() {
          super.voiceRevoked();
          ...
      }
  });

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

      publicvoidvoiceRevoked(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 .

Solution 3:

Firstly, join a chat room:

publicMultiUserChatjoinMultiUserChat(String user, String roomsName,
        String password) {
    if (getConnection() == null)
        returnnull;
    try {
        MultiUserChat muc = newMultiUserChat(getConnection(), roomsName
                + "@conference." + getConnection().getServiceName());
        DiscussionHistory history = newDiscussionHistory();
        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........");
        returnnull;
    }
}

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;

publicclassTaxiMultiListenerimplementsPacketListener {
    @OverridepublicvoidprocessPacket(Packet packet) {
        Messagemessage= (Message) packet;
        Stringbody= message.getBody();
    }
}

Finally, call the Listener using MultiUserChat:

multiUserChat.addMessageListener(newTaxiMultiListener());

Post a Comment for "Smack Presence Listener In Multi User Chat"