继续上一章smack的讲述,聊天室的操作,聊天室也是openfire提供的一种形式,类似qq的讨论组,并非群,可以多人加入进来进行群聊。
public boolean createChatRoom(String roomName,String subject){
boolean result = false;
try{
MultiUserChat muc = new MultiUserChat(connectManager.getConnection(), roomName+"@conference."+connectManager.getDomain());
muc.create(connectManager.getNode());
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for (Iterator fields = form.getFields(); fields.hasNext();) {
FormField field = (FormField) fields.next();
if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
submitForm.setDefaultAnswer(field.getVariable());
}
}
// submitForm.setAnswer("muc#roomconfig_roomname", "wwh");
// submitForm.setAnswer("muc#roomconfig_roomdesc", "wwh2222");
List list = new ArrayList();
list.add("20");
submitForm.setAnswer("muc#roomconfig_maxusers", list);
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
submitForm.setAnswer("muc#roomconfig_membersonly", false);
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
submitForm.setAnswer("x-muc#roomconfig_registration", false);
muc.sendConfigurationForm(submitForm);
muc.changeSubject(subject);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
创建聊天室,主要就是在创建的时候根据需求对聊天室的一些属性进行设置
顺便把包也列出来:
public boolean joinChatRoom(String roomName){
boolean result = false;
try {
MultiUserChat muc = new MultiUserChat(connectManager.getConnection(), roomName+"@conference."+connectManager.getDomain());
//添加消息监听
muc.addMessageListener(new UcGroupChatManagerListener(this));
//添加其他聊天室人员状态变化监听
muc.addParticipantStatusListener(new UcParticipantStatusListener(this,roomName));
//添加直接在聊天室的状态变化监听
muc.addUserStatusListener(new UcUserStatusListener(this,roomName));
//添加邀请被拒绝的监听
muc.addInvitationRejectionListener(new UcInvitationRejectionListener(this,roomName));
muc.addPresenceInterceptor(new UcPresenceInterceptor());
//设置历史消息数量
DiscussionHistory history = new DiscussionHistory();
history.setMaxChars(0);
muc.join(connectManager.getNode(),null, history,SmackConfiguration.getPacketReplyTimeout());
// muc.grantOwnership("10110@kfas1/Spark 2.6.3");//对聊天室一些权限的授予,与删除
// muc.grantAdmin("10110@kfas1/Spark 2.6.3");
// muc.grantMembership("10110@kfas1/Spark 2.6.3");
// muc.grantModerator("10110");
// muc.grantVoice("10110");
// muc.revokeVoice("10110");
muc.revokeAdmin("10110@kfas1/Spark 2.6.3");
mucMap.put(roomName, muc);
result = true;
} catch (XMPPException e) {
log.error("加入聊天室异常",e);
}
return result;
}
public boolean departChatRoom(String roomName){
boolean result = false;
MultiUserChat muc = mucMap.get(roomName);
if(muc!=null){
muc.leave();
mucMap.remove(roomName);
result = true;
}
return result;
}
type="unavailable">
public boolean inviteChatRoom(String roomName,String toJid,String reason){
boolean result = false;
MultiUserChat muc = mucMap.get(roomName);
if(muc!=null){
muc.invite(toJid,reason);
result = true;
}
return result;
}
public void rejectInvite(String room,String inviter,String reason){
MultiUserChat.decline(connectManager.getConnection(), room, inviter, reason);
}
public List findMulitUser(String roomName) {
MultiUserChat muc = mucMap.get(roomName);
List listUser = new ArrayList();
if(muc!=null){
Iterator it = muc.getOccupants();
while (it.hasNext()) {
String name = StringUtils.parseResource(it.next());
listUser.add(name);
}
}
return listUser;
}
public boolean sendChatMessage(String roomName,String content){
boolean result = false;
try {
MultiUserChat muc = mucMap.get(roomName);
if(muc!=null){
Message message = new Message();
message.setBody(content);
message.setTo(muc.getRoom());
message.setType(Message.Type.groupchat);
message.setSubject(MSG_SUBJECT);
muc.sendMessage(message);
result =true;
}
} catch (XMPPException e) {
log.error("发送消息异常",e);
}
return result;
}
测试聊天室消息
实例在上面加入聊天室已经给出了。
下一章讲一下在这里也有涉及的监听器。