openfire smack包聊天室

繼續上一章smack的講述,聊天室的操做,聊天室也是openfire提供的一種形式,相似qq的討論組,並不是羣,能夠多人加入進來進行羣聊。 java

1.建立一個聊天室

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;
	}
建立聊天室,主要就是在建立的時候根據需求對聊天室的一些屬性進行設置
房間名稱 text-single muc#roomconfig_roomname
描述 text-single muc#roomconfig_roomdesc
容許佔有者更改主題 boolean muc#roomconfig_changesubject
最大房間佔有者人數 list-single muc#roomconfig_maxusers
其 Presence 是 Broadcast 的角色 list-multi muc#roomconfig_presencebroadcast
列出目錄中的房間 boolean muc#roomconfig_publicroom
房間是持久的 boolean muc#roomconfig_persistentroom
房間是適度的 boolean muc#roomconfig_moderatedroom
房間僅對成員開放 boolean muc#roomconfig_membersonly
容許佔有者邀請其餘人 boolean muc#roomconfig_allowinvites
須要密碼才能進入房間 boolean muc#roomconfig_passwordprotectedroom
密碼 text-private muc#roomconfig_roomsecret
可以發現佔有者真實 JID 的角色 list-single muc#roomconfig_whois
登陸房間對話 boolean muc#roomconfig_enablelogging
僅容許註冊的暱稱登陸 boolean x-muc#roomconfig_reservednick
容許使用者修改暱稱 boolean x-muc#roomconfig_canchangenick
容許用戶註冊房間 boolean x-muc#roomconfig_registration
房間管理員 jid-multi muc#roomconfig_roomadmins
房間擁有者 jid-multi muc#roomconfig_roomowners

順便把包也列出來: 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>


2.加入聊天室

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>


3.離開聊天室

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>


4.邀請加入聊天室

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>


5.拒絕加入聊天室

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>


6.查詢會議室成員名字 

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;  
    }


7.發送聊天室消息

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>


8.聊天室中角色,關係

實例在上面加入聊天室已經給出了。

角色:
        moderator:主持人 (能夠進行踢人,禁止發言等受權)
        participant:參與者 (能夠發言)
        visitor:訪客(不能發言)
        none:無
關係:
     owner:擁有者
        admin:管理員
        member:會員
        outcast:被禁止加入的
        none:無

在smcak裏面的受權:(角色會隨着關係變化,反之不會)
授予擁有者,管理員,會員,發言,主持人

被授予擁有,管理的時候,角色自動變成:主持人
被授予會員的時候,角色變成:參與者
被授予主持人,關係不會變化
被禁止發言:角色變成訪客

聊天室的主要操做都在這裏講述,其實可能還有一些其餘的,你們能夠多看看smack的源碼就會發現更多的隱藏協議。

下一章講一下在這裏也有涉及的監聽器。

相關文章
相關標籤/搜索