java+smack+openfire即时通讯Im(四)

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){
        //默认使用创建人用户名+时间戳为房间ID
        String 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 room
            Form 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(五)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值