改造MUC实现Openfire群

我的Openfire群实现思路:

1、群和群成员,要保存到表中。

2、拉取群列表和群成员列表,均从DB中查询返回。

3、抛弃老外的“进房间,要发Presence ”。只要此人一上线,就模似一个Presence进行joinRoom,进入他的各群房间。

     多了解LocalMUCRoom 类中:public LocalMUCRole joinRoom(String nickname, String password, HistoryRequest historyRequest, LocalMUCUser user, Presence presence)

    我的模似代码如下:

View Code
 1  /**
 2       * 模似用户进群(含呢称、含Precense、初始化角色LocalMUCRole)
 3       *  @param  roomId
 4       *  @param  packet
 5       *  @return
 6        */
 7      public MUCRole getRolesByRoomId( long roomId, Packet packet)
 8     {
 9         MUCRole role =  null;
10          try {
11              //  Get or create the room  获取该群
12              MUCRoom room = server.getChatRoom(roomId, packet.getFrom());
13             
14              // 从数据库中查询他的姓名作为昵称(得自己实现)
15              String nickname =  new MUCRoomServiceDao().getUserNickname(packet.getFrom().getNode());
16              if(nickname ==  null)
17             {
18                  if(packet.getFrom().getResource() !=  null)
19                 {
20                     nickname = packet.getFrom().getResource();
21                 }
22                  else 
23                 {
24                     nickname = packet.getFrom().getNode();
25                 }
26             }
27             
28             HistoryRequest historyRequest =  null;
29             String password =  null;
30             
31              // 构建成员进入群的Presence
32              Presence presence =  new Presence();
33             presence.setTo(room.getJID().toBareJID() + "/" + nickname);    
34             presence.setFrom(packet.getFrom());
35             PacketExtension extension =  new PacketExtension("x",  http://jabber.org/protocol/muc);
36             presence.addExtension(extension);
37             
38              //  The user joins the room 用户进入群
39              role = room.joinRoom(nickname,
40                     password,
41                     historyRequest,
42                      this,
43                     presence);
44             
45              //  If the client that created the room is non-MUC compliant then
46               //  unlock the room thus creating an "instant" room
47               // if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
48               if (room.isLocked() && !room.isManuallyLocked()) {
49                 room.unlock(role);
50                  // server.chatRoomAdded((LocalMUCRoom)room);
51              }
52             
53             addRole(roomId, (LocalMUCRole)role); // 添加“用户在某个群中的角色”
54              
55         }
56          catch (UnauthorizedException e) {
57             sendErrorPacket(packet, PacketError.Condition.not_authorized);
58         }
59          catch (ServiceUnavailableException e) {
60             sendErrorPacket(packet, PacketError.Condition.service_unavailable);
61         }
62          catch (UserAlreadyExistsException e) {
63             sendErrorPacket(packet, PacketError.Condition.conflict);
64         }
65          catch (RoomLockedException e) {
66             sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
67         }
68          catch (ForbiddenException e) {
69             sendErrorPacket(packet, PacketError.Condition.forbidden);
70         }
71          catch (RegistrationRequiredException e) {
72             sendErrorPacket(packet, PacketError.Condition.registration_required);
73         }
74          catch (ConflictException e) {
75             sendErrorPacket(packet, PacketError.Condition.conflict);
76         }
77          catch (NotAcceptableException e) {
78             sendErrorPacket(packet, PacketError.Condition.not_acceptable);
79         }
80          catch (NotAllowedException e) {
81             sendErrorPacket(packet, PacketError.Condition.not_allowed);
82         }        
83          return role;
84     }

 4、抛弃老外的“以昵称为Key 缓存群成员”。改为以帐号为Key。

     多了解 LocalMUCRoom 类中:private Map<String,MUCRole> occupants = new ConcurrentHashMap<String, MUCRole>();

     在joinRoom办法中:    

View Code
1  // 以JID作为缓存的key
2          JID userJid = user.getAddress();
3          if (userJid !=  null) {
4             occupants.put(userJid.toBareJID(), joinRole);
5         }

 5、详细了解 LocalMUCRoom、LocalMUCUser、LocalMUCRole这三个类,各类中的数据成员、方法。 

 

你可能感兴趣的:(openfire)