java+smack+openfire即时通讯Im(四)
- 创建群聊
- 加入群聊
创建群聊
前面的单聊完成以后,就开始群聊的功能把,首先是创建群聊:
后端进行群组创建:
/***** <B>方法名称:</B>createMutiUserChat<BR>* <B>概要说明:</B>创建群聊<BR>** @param userName* @param password* @param roomName* @param roomDesc* @return void** @author ZhangYH* @date 2020/06/17 10:18:08*/public static String createMutiUserChat(String userName, String password,String roomName,String roomDesc){//默认使用创建人用户名+时间戳为房间IDString roomId = userName+"_"+System.currentTimeMillis();try {XMPPTCPConnection connection = initOpenfireConnect();connection.login(userName,password);//获取房间管理对象MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);//创建一个房间MultiUserChat muc = manager.getMultiUserChat(roomId+"@conference."+OPENFIRE_SERVICE_NAME);muc.create(userName);// User1 (which is the room owner) configures the room as a moderated roomForm form = muc.getConfigurationForm();Form answerForm = form.createAnswerForm();//向提交的表单添加默认答复,获取房间的默认设置菜单for(FormField field : form.getFields() ){if(!FormField.Type.hidden.name().equals(field.getType()) && field.getVariable() != null) {answerForm.setDefaultAnswer(field.getVariable());}}//muc#//房间名称answerForm.setAnswer(FormField.FORM_TYPE, "http://jabber.org/protocol/muc#roomconfig");//设置房间名称answerForm.setAnswer("muc#roomconfig_roomname",roomName);//设置房间描述answerForm.setAnswer("muc#roomconfig_roomdesc", roomDesc);//是否允许修改主题answerForm.setAnswer("muc#roomconfig_changesubject", true);//设置房间最大用户数List<String> maxusers = new ArrayList<String>();maxusers.add("100");answerForm.setAnswer("muc#roomconfig_maxusers", maxusers);List<String> cast_values = new ArrayList<String>();cast_values.add("moderator");cast_values.add("participant");cast_values.add("visitor");answerForm.setAnswer("muc#roomconfig_presencebroadcast", cast_values);//设置为公共房间answerForm.setAnswer("muc#roomconfig_publicroom", true);//设置为永久房间answerForm.setAnswer("muc#roomconfig_persistentroom", true);//允许修改昵称answerForm.setAnswer("x-muc#roomconfig_canchangenick", true);//允许用户登录注册房间answerForm.setAnswer("x-muc#roomconfig_registration", true);muc.sendConfigurationForm(answerForm);muc.join(userName);//关闭连接closeConnection(connection);return roomId;}catch (Exception e){e.printStackTrace();}return roomId;}
加入群聊
/***** <B>方法名称:</B>joinMultiUserChat<BR>* <B>概要说明:</B>加入群聊<BR>** @param userName* @param password* @param roomId* @return void** @author ZhangYH* @date 2020/06/17 10:30:28*/public static void joinMultiUserChat(String userName, String password, String roomId) {try {XMPPTCPConnection connection = initOpenfireConnect();connection.login(userName,password);// 使用XMPPConnection创建一个MultiUserChat窗口MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);//获取会议室信息MultiUserChat muc = manager.getMultiUserChat(roomId+"@conference."+OPENFIRE_SERVICE_NAME);// 聊天室服务将会决定要接受的历史记录数量DiscussionHistory history = new DiscussionHistory();history.setMaxStanzas(0);//history.setSince(new Date());// 用户加入聊天室muc.join(userName, password, history, System.currentTimeMillis());System.out.println("群聊加入成功........");//关闭连接closeConnection(connection);}catch (Exception e){e.printStackTrace();}}
注:openfire的群聊机制是这样的:建群的时候必须要设置群组信息持久化(answerForm.setAnswer(“muc#roomconfig_persistentroom”, true);),要不然过一段时间群聊会自动删除的,还有就是添加成员进入群聊以后,当成员下线以后会自动将下线的成员踢出群聊。
上一篇:java+smack+openfire即时通讯Im(三)
下一篇:java+smack+openfire即时通讯Im(五)