openfire muc 移除成员

muc添加成员到数据库可参考 将Openfire中的MUC改造成类似QQ群一样的永久群

插件

 

插件是一位大神参考第一篇文章改进后编写的插件,进测试可以直接使用。

 

----------------------------------------分割线------------------------------------------------------------

本文主要介绍如何移除成员

基本思路:

1、openfire控制台的muc-room-occupants.jsp页面有踢出成员功能

jsp源码

 

// Kick nick specified
    if (kick != null) {
        MUCRole role = room.getOccupant(nickName);
        if (role != null) {
            try {
                room.kickOccupant(role.getUserAddress(), XMPPServer.getInstance().createJID(webManager.getUser().getUsername(), null), null, "");
                // Log the event
                webManager.logEvent("kicked MUC occupant "+nickName+" from "+roomName, null);
                // Done, so redirect
                response.sendRedirect("muc-room-occupants.jsp?roomJID="+URLEncoder.encode(room.getJID().toBareJID(), "UTF-8")+"&nickName="+URLEncoder.encode(role.getNickname(), "UTF-8")+"&deletesuccess=true");
                return;
            }
            catch (NotAllowedException e) {
                // Done, so redirect
                response.sendRedirect("muc-room-occupants.jsp?roomJID="+URLEncoder.encode(room.getJID().toBareJID(), "UTF-8")+"&nickName="+URLEncoder.encode(role.getNickname(), "UTF-8")+"&deletefailed=true");
                return;
            }
        }
    }



 

2、可以看到核心的kickOccupant方法,我们来看实现类LocalMUCRoom中的具体实现方法

 

 

public Presence kickOccupant(JID jid, JID actorJID, String actorNickname, String reason)
            throws NotAllowedException {
        System.out.println("jid="+jid.toString()+" ==== actorJID="+actorJID);
        // Update the presence with the new role and inform all occupants
        Presence updatedPresence = changeOccupantRole(jid, MUCRole.Role.none);//改变成员在房间的角色
        if (updatedPresence != null) {
            Element frag = updatedPresence.getChildElement(
                    "x", "http://jabber.org/protocol/muc#user");

            // Add the status code 307 that indicates that the user was kicked
            frag.addElement("status").addAttribute("code", "307");
            // Add the reason why the user was kicked
            if (reason != null && reason.trim().length() > 0) {
                frag.element("item").addElement("reason").setText(reason);
            }

            // Effectively kick the occupant from the room
            kickPresence(updatedPresence, actorJID, actorNickname);//移除成员

            //Inform the other occupants that user has been kicked
            broadcastPresence(updatedPresence, false);//广播
        }
        return updatedPresence;
    }



 

 

主要在上述方法添加删除数据库方法就可以了。

但是发现如果房间设置只允许成员加入,但是明明移除了成员,数据库也删除了,但是在服务器未重启的情况下用户还是可以进入房间。

研究进入房间的源码LocalMucRoom的joinRoom方法,发现有判断 members.includesKey(bareJID)。

   所以我们可以在删除数据库后把成员remove。

   3、还需要判断一点,用户是否在房间内,如果不在只需删除数据库。

------------------------以下是部分源码截图------------------------------------------------------------------

1、插件

2、方法

 

 

3、kickMember方法

 

操作数据库的代码就不在贴出了。

 

ps:第一次写博客,写的不好,请多多指教

转载于:https://www.cnblogs.com/fengIdea/p/6856020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值