在Openfire+Smack开发IM应用中,百度了N久,但还是每个人作者给的参数都不一样。在Openfire所有文档中,找了N久,还是找不到房间的参数。只要采用代码的办法来处理:
不用多说,具体实现办法如下:
public static boolean createMultiUserChat(String roomJID,String roomName,String password)
{
XMPPTCPConnection conn=getInstance();
MultiUserChatManager mcum=MultiUserChatManager.getInstanceFor(conn);
MultiUserChat mcu=mcum.getMultiUserChat(roomJID);
boolean b=false;
try {
mcu.create(roomName);
Form form=mcu.getConfigurationForm();
Form submitForm=form.createAnswerForm();
List<FormField> fields=form.getFields();
for(FormField field:fields)
{
if(!FormField.Type.hidden.equals(field.getType())&&field.getVariable()!=null)
{
submitForm.setDefaultAnswer(field.getVariable());
System.out.println(field.getVariable()+" "+field.getType().toString()+" "+field.getLabel());
}
}
// 设置聊天室的新拥有者
List<String> owners = new ArrayList<String>();
owners.add(conn.getUser());// 用户JID
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// 设置聊天室是持久聊天室,即将要被保存下来
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
// 房间仅对成员开放
submitForm.setAnswer("muc#roomconfig_membersonly", false);
// 允许占有者邀请其他人
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
if (!password.equals("")) {
// 进入是否需要密码
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom",
true);
// 设置进入密码
submitForm.setAnswer("muc#roomconfig_roomsecret", password);
}
// 能够发现占有者真实 JID 的角色
// submitForm.setAnswer("muc#roomconfig_whois", "anyone");
// 登录房间对话
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);
// 发送已完成的表单(有默认值)到服务器来配置聊天室
mcu.sendConfigurationForm(submitForm);
mcu.join(roomName);//加入房间
// try
// {
// String[] jid=conn.getUser().split("\\/");
// mcu.grantAdmin(jid[0]);
// }
// catch(Exception ex)
// {
// JOptionPane.showConfirmDialog(null, ex.getMessage());
// }
b=true;
} catch (NoResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
b=false;
} catch (XMPPErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
b=false;
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
b=false;
}
return b;
}
这样就可以将房间表单参数全部打印出来,就可以知道什么意思了!
muc#roomconfig_roomname text-single 房间名称
muc#roomconfig_roomdesc text-single 描述
muc#roomconfig_changesubject boolean 允许占有者更改主题
muc#roomconfig_maxusers list-single 最大房间占有者人数
muc#roomconfig_presencebroadcast list-multi 其 Presence 是 Broadcast 的角色
muc#roomconfig_publicroom boolean 列出目录中的房间
muc#roomconfig_persistentroom boolean 房间是持久的
muc#roomconfig_moderatedroom boolean 房间是适度的
muc#roomconfig_membersonly boolean 房间仅对成员开放
muc#roomconfig_allowinvites boolean 允许占有者邀请其他人
muc#roomconfig_passwordprotectedroom boolean 需要密码才能进入房间
muc#roomconfig_roomsecret text-private 密码
muc#roomconfig_whois list-single 能够发现占有者真实 JID 的角色
muc#roomconfig_enablelogging boolean 登录房间对话
x-muc#roomconfig_reservednick boolean 仅允许注册的昵称登录
x-muc#roomconfig_canchangenick boolean 允许使用者修改昵称
x-muc#roomconfig_registration boolean 允许用户注册房间
muc#roomconfig_roomadmins jid-multi 房间管理员
muc#roomconfig_roomowners jid-multi 房间拥有者