smack 4.1.1版本对群聊修改了很多,MultUserChat的构造函数修改成了私有,以前通过new MultUserChat创建聊天室,现在通过MultUserChatMananger先通过roomId初始化一个聊天室.
1 String roomId = "test003";
2 String name="test_group_chat";
3 String desc = "the test group chat";
4 String nick="test";
5
6 try {
7
8 //获取房间管理对象
9 MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(XmppConnectionManager.getInstance().getConnection());
10 //创建一个房间
11 MultiUserChat muc = manager.getMultiUserChat(StringUtils.getGroupRootFullId(roomId));
12 muc.create(nick);
13
14 // User1 (which is the room owner) configures the room as a moderated room
15 Form form = muc.getConfigurationForm();
16
17 Form answerForm = form.createAnswerForm();
18 //向提交的表单添加默认答复,获取房间的默认设置菜单
19 for(FormField field : form.getFields() ){
20 if(!FormField.Type.hidden.name().equals(field.getType()) && field.getVariable() != null) {
21 answerForm.setDefaultAnswer(field.getVariable());
22 }
23 }
24
25 //muc#
26 //房间名称
27 answerForm.setAnswer(FormField.FORM_TYPE, "http://jabber.org/protocol/muc#roomconfig");
28 //设置房间名称
29 answerForm.setAnswer("muc#roomconfig_roomname",name);
30 //设置房间描述
31 answerForm.setAnswer("muc#roomconfig_roomdesc", desc);
32 //是否允许修改主题
33 answerForm.setAnswer("muc#roomconfig_changesubject", true);
34
35 //设置房间最大用户数
36 List<String> maxusers = new ArrayList<String>();
37 maxusers.add("50");
38 answerForm.setAnswer("muc#roomconfig_maxusers", maxusers);
39
40
41 List<String> cast_values = new ArrayList<String>();
42 cast_values.add("moderator");
43 cast_values.add("participant");
44 cast_values.add("visitor");
45 answerForm.setAnswer("muc#roomconfig_presencebroadcast", cast_values);
46 //设置为公共房间
47 answerForm.setAnswer("muc#roomconfig_publicroom", true);
48 //设置为永久房间
49 answerForm.setAnswer("muc#roomconfig_persistentroom", true);
50 //允许修改昵称
51 answerForm.setAnswer("x-muc#roomconfig_canchangenick", true);
52 //允许用户登录注册房间
53 answerForm.setAnswer("x-muc#roomconfig_registration", true);
54
55
56 muc.sendConfigurationForm(answerForm);
57 muc.join(nick);
58
59 } catch (XMPPException e) {
60 e.printStackTrace();
61 }catch (SmackException.NoResponseException e) {
62 e.printStackTrace();
63 } catch (SmackException.NotConnectedException e) {
64 e.printStackTrace();
65 }catch (SmackException e) {
66 e.printStackTrace();
67 }