多用户聊天中的 smack 状态侦听器
smack presence listener in multi user chat
多用户聊天中的 smack 状态侦听器未被调用。使用 Smack Api 登录,然后添加 roster.addRosterListener(mRoasterListener); 但当聊天室其他用户的状态发生变化时无法成功收听。我尝试了以下代码来让存在监听器工作:
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));
和这个 class 监听状态变化...
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);
}
}
我尝试了 Whosebug 提供的许多链接,但都没有成功。
请帮忙!
对于多用户聊天,您不必使用花名册,因为遇到不在花名册中的人是很正常的。
想知道谁在muc里,先问住户:
muc.join(user,password);
List<String> occupantsAtJoinTime = muc.getOccupants();
for (String occupant : occupantsAtJoinTime)
{
System.out.println("occupant: "+occupant);
//actions
}
然后,为了保持 Occupants 列表更新,向您的 muc 注册一个 DefaultParticipantStatusListener 并定义该 Listner:
muc.addParticipantStatusListener(new CustomParticipantStatusListner());
定义为(有很多方法可以根据需要实现):
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)
}
}
所有这些都带有 smack 4.1.7
这是关于多用户聊天中的管理角色修改。
此示例显示如何向访问者授予语音并侦听通知事件:
// 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");
详情可参考http://web.mit.edu/svalente/lib/smack_3_0_4/documentation/extensions/muc.html.
首先,加入聊天室:
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;
}
}
然后,使用 MultiChatUser 发送消息:
try {
multiUserChat.sendMessage(message);
} catch (XMPPException e) {
e.printStackTrace();
}
添加监听器:
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();
}
}
最后,使用 MultiUserChat 调用监听器:
multiUserChat.addMessageListener(new TaxiMultiListener());
多用户聊天中的 smack 状态侦听器未被调用。使用 Smack Api 登录,然后添加 roster.addRosterListener(mRoasterListener); 但当聊天室其他用户的状态发生变化时无法成功收听。我尝试了以下代码来让存在监听器工作:
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));
和这个 class 监听状态变化...
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);
}
}
我尝试了 Whosebug 提供的许多链接,但都没有成功。 请帮忙!
对于多用户聊天,您不必使用花名册,因为遇到不在花名册中的人是很正常的。
想知道谁在muc里,先问住户:
muc.join(user,password);
List<String> occupantsAtJoinTime = muc.getOccupants();
for (String occupant : occupantsAtJoinTime)
{
System.out.println("occupant: "+occupant);
//actions
}
然后,为了保持 Occupants 列表更新,向您的 muc 注册一个 DefaultParticipantStatusListener 并定义该 Listner:
muc.addParticipantStatusListener(new CustomParticipantStatusListner());
定义为(有很多方法可以根据需要实现):
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)
}
}
所有这些都带有 smack 4.1.7
这是关于多用户聊天中的管理角色修改。 此示例显示如何向访问者授予语音并侦听通知事件:
// 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");
详情可参考http://web.mit.edu/svalente/lib/smack_3_0_4/documentation/extensions/muc.html.
首先,加入聊天室:
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;
}
}
然后,使用 MultiChatUser 发送消息:
try {
multiUserChat.sendMessage(message);
} catch (XMPPException e) {
e.printStackTrace();
}
添加监听器:
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();
}
}
最后,使用 MultiUserChat 调用监听器:
multiUserChat.addMessageListener(new TaxiMultiListener());