改造MUC实现Openfire群
我的Openfire群實現(xiàn)思路:
1、群和群成員,要保存到表中。
2、拉取群列表和群成員列表,均從DB中查詢返回。
3、拋棄老外的“進房間,要發(fā)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?????????????//從數(shù)據(jù)庫中查詢他的姓名作為昵稱(得自己實現(xiàn))
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?????????????//構(gòu)建成員進入群的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作為緩存的key2?????????JID?userJid?=?user.getAddress();
3?????????if?(userJid?!=?null)?{
4?????????????occupants.put(userJid.toBareJID(),?joinRole);
5?????????}
?5、詳細了解 LocalMUCRoom、LocalMUCUser、LocalMUCRole這三個類,各類中的數(shù)據(jù)成員、方法。?
?
轉(zhuǎn)載于:https://www.cnblogs.com/huazai8204/archive/2011/12/31/2309022.html
總結(jié)
以上是生活随笔為你收集整理的改造MUC实现Openfire群的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HttpApplication类及派生的
- 下一篇: 学习:SQL Server的BUILTI