繼續上一章smack的講述,聊天室的操做,聊天室也是openfire提供的一種形式,相似qq的討論組,並不是羣,能夠多人加入進來進行羣聊。 java
public boolean createChatRoom(String roomName,String subject){ boolean result = false; try{ MultiUserChat muc = new MultiUserChat(connectManager.getConnection(), roomName+"@conference."+connectManager.getDomain()); muc.create(connectManager.getNode()); Form form = muc.getConfigurationForm(); Form submitForm = form.createAnswerForm(); for (Iterator fields = form.getFields(); fields.hasNext();) { FormField field = (FormField) fields.next(); if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) { submitForm.setDefaultAnswer(field.getVariable()); } } // submitForm.setAnswer("muc#roomconfig_roomname", "wwh"); // submitForm.setAnswer("muc#roomconfig_roomdesc", "wwh2222"); List list = new ArrayList(); list.add("20"); submitForm.setAnswer("muc#roomconfig_maxusers", list); submitForm.setAnswer("muc#roomconfig_persistentroom", true); submitForm.setAnswer("muc#roomconfig_membersonly", false); submitForm.setAnswer("muc#roomconfig_allowinvites", true); submitForm.setAnswer("muc#roomconfig_enablelogging", true); submitForm.setAnswer("x-muc#roomconfig_reservednick", true); submitForm.setAnswer("x-muc#roomconfig_canchangenick", false); submitForm.setAnswer("x-muc#roomconfig_registration", false); muc.sendConfigurationForm(submitForm); muc.changeSubject(subject); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }建立聊天室,主要就是在建立的時候根據需求對聊天室的一些屬性進行設置
順便把包也列出來: c#
<presence id="5ryxs-4" <a href="mailto:to=\" testroom@conference.wenwh-pc="" 10110\"="">" >to="testroom@conference.wenwh-pc/10110"> 測試
<x xmlns="http://jabber.org/protocol/muc"></x> this
</presence> spa
<iq id="5ryxs-5" to="testroom@conference.wenwh-pc" type="get"> code
<query xmlns="http://jabber.org/protocol/muc#owner"></query> orm
</iq> xml
<iq id="5ryxs-6" to="testroom@conference.wenwh-pc" type="set"> ip
<query xmlns="http://jabber.org/protocol/muc#owner"> ci
<x xmlns="jabber:x:data" type="submit"><field var="FORM_TYPE" type="hidden">
<value>http://jabber.org/protocol/muc#roomconfig</value></field><field var="muc#roomconfig_persistentroom" type="boolean"><value>1</value></field><field var="muc#roomconfig_membersonly" type="boolean"><value>0</value></field><field var="muc#roomconfig_allowinvites" type="boolean"><value>1</value></field><field var="muc#roomconfig_enablelogging" type="boolean"><value>1</value></field><field var="x-muc#roomconfig_reservednick" type="boolean"><value>1</value></field><field var="x-muc#roomconfig_canchangenick" type="boolean"><value>0</value></field><field var="x-muc#roomconfig_registration" type="boolean"><value>0</value></field>
</x>
</query>
</iq>
public boolean joinChatRoom(String roomName){ boolean result = false; try { MultiUserChat muc = new MultiUserChat(connectManager.getConnection(), roomName+"@conference."+connectManager.getDomain()); //添加消息監聽 muc.addMessageListener(new UcGroupChatManagerListener(this)); //添加其餘聊天室人員狀態變化監聽 muc.addParticipantStatusListener(new UcParticipantStatusListener(this,roomName)); //添加直接在聊天室的狀態變化監聽 muc.addUserStatusListener(new UcUserStatusListener(this,roomName)); //添加邀請被拒絕的監聽 muc.addInvitationRejectionListener(new UcInvitationRejectionListener(this,roomName)); muc.addPresenceInterceptor(new UcPresenceInterceptor()); //設置歷史消息數量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); muc.join(connectManager.getNode(),null, history,SmackConfiguration.getPacketReplyTimeout()); // muc.grantOwnership("10110@kfas1/Spark 2.6.3");//對聊天室一些權限的授予,與刪除 // muc.grantAdmin("10110@kfas1/Spark 2.6.3"); // muc.grantMembership("10110@kfas1/Spark 2.6.3"); // muc.grantModerator("10110"); // muc.grantVoice("10110"); // muc.revokeVoice("10110"); muc.revokeAdmin("10110@kfas1/Spark 2.6.3"); mucMap.put(roomName, muc); result = true; } catch (XMPPException e) { log.error("加入聊天室異常",e); } return result; }
<presence id="CC26U-4" to="testroom@conference.wenwh-pc/10110">
<x xmlns="http://jabber.org/protocol/muc"><history maxchars="0"/></x>
</presence>
public boolean departChatRoom(String roomName){ boolean result = false; MultiUserChat muc = mucMap.get(roomName); if(muc!=null){ muc.leave(); mucMap.remove(roomName); result = true; } return result; }
<presence id="fmmBY-5" to="testroom@conference.wenwh-pc/10110"
type="unavailable"></presence>
public boolean inviteChatRoom(String roomName,String toJid,String reason){ boolean result = false; MultiUserChat muc = mucMap.get(roomName); if(muc!=null){ muc.invite(toJid,reason); result = true; } return result; }
<message id="34B0W-5「 to="testroom@conference.wenwh-pc/10110"
<x xmlns="http://jabber.org/protocol/muc#user">
<invite to="10111@wenwh-pc"><reason>一塊兒聊天</reason></invite>
</x>
</message>
public void rejectInvite(String room,String inviter,String reason){ MultiUserChat.decline(connectManager.getConnection(), room, inviter, reason); }
<message from="testroom@conference.wenwh-pc" to="10110@wenwh-pc"><x xmlns="http://jabber.org/protocol/muc#user"><decline from="10111@wenwh-pc"><reason>No thank you</reason></decline></x>
public List<String> findMulitUser(String roomName) { MultiUserChat muc = mucMap.get(roomName); List<String> listUser = new ArrayList<String>(); if(muc!=null){ Iterator<String> it = muc.getOccupants(); while (it.hasNext()) { String name = StringUtils.parseResource(it.next()); listUser.add(name); } } return listUser; }
public boolean sendChatMessage(String roomName,String content){ boolean result = false; try { MultiUserChat muc = mucMap.get(roomName); if(muc!=null){ Message message = new Message(); message.setBody(content); message.setTo(muc.getRoom()); message.setType(Message.Type.groupchat); message.setSubject(MSG_SUBJECT); muc.sendMessage(message); result =true; } } catch (XMPPException e) { log.error("發送消息異常",e); } return result; }
<message id="f6tkr-5" to="testroom@conference.wenwh-pc" type="groupchat">
<body>測試聊天室消息</body></message>
實例在上面加入聊天室已經給出了。
下一章講一下在這裏也有涉及的監聽器。