继续上一章smack的讲述,聊天室的操作,聊天室也是openfire提供的一种形式,类似qq的讨论组,并非群,可以多人加入进来进行群聊。
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; }创建聊天室,主要就是在创建的时候根据需求对聊天室的一些属性进行设置
顺便把包也列出来:
<presence id="5ryxs-4" <a href="mailto:to=\" [email protected]="" 10110\"="">" >to="[email protected]/10110">
<x xmlns="http://jabber.org/protocol/muc"></x>
</presence>
<iq id="5ryxs-5" to="[email protected]" type="get">
<query xmlns="http://jabber.org/protocol/muc#owner"></query>
</iq>
<iq id="5ryxs-6" to="[email protected]" type="set">
<query xmlns="http://jabber.org/protocol/muc#owner">
<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="[email protected]/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="[email protected]/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="[email protected]/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="[email protected]" 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="[email protected]" type="groupchat">
<body>测试聊天室消息</body></message>
实例在上面加入聊天室已经给出了。
下一章讲一下在这里也有涉及的监听器。