java smack 例子_java+smack+openfire即时通讯Im(四)
### java+smack+openfire即時通訊Im(四) ###
* 創建群聊
* 加入群聊
# 創建群聊 #
前面的單聊完成以后,就開始群聊的功能把,首先是創建群聊:
后端進行群組創建:
/***
*
* 方法名稱:createMutiUserChat
* 概要說明:創建群聊
*
* @param userName
* @param password
* @param roomName
* @param roomDesc
* @return void
*
* @author ZhangYH
* @date 2020/06/17 10:18:08
*/
public static String createMutiUserChat(String userName, String password,String roomName,String roomDesc){
//默認使用創建人用戶名+時間戳為房間ID
String roomId = userName+"_"+System.currentTimeMillis();
try {
XMPPTCPConnection connection = initOpenfireConnect();
connection.login(userName,password);
//獲取房間管理對象
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
//創建一個房間
MultiUserChat muc = manager.getMultiUserChat(roomId+"@conference."+OPENFIRE_SERVICE_NAME);
muc.create(userName);
// User1 (which is the room owner) configures the room as a moderated room
Form form = muc.getConfigurationForm();
Form answerForm = form.createAnswerForm();
//向提交的表單添加默認答復,獲取房間的默認設置菜單
for(FormField field : form.getFields() ){
if(!FormField.Type.hidden.name().equals(field.getType()) && field.getVariable() != null) {
answerForm.setDefaultAnswer(field.getVariable());
}
}
//muc#
//房間名稱
answerForm.setAnswer(FormField.FORM_TYPE, "http://jabber.org/protocol/muc#roomconfig");
//設置房間名稱
answerForm.setAnswer("muc#roomconfig_roomname",roomName);
//設置房間描述
answerForm.setAnswer("muc#roomconfig_roomdesc", roomDesc);
//是否允許修改主題
answerForm.setAnswer("muc#roomconfig_changesubject", true);
//設置房間最大用戶數
List maxusers = new ArrayList();
maxusers.add("100");
answerForm.setAnswer("muc#roomconfig_maxusers", maxusers);
List cast_values = new ArrayList();
cast_values.add("moderator");
cast_values.add("participant");
cast_values.add("visitor");
answerForm.setAnswer("muc#roomconfig_presencebroadcast", cast_values);
//設置為公共房間
answerForm.setAnswer("muc#roomconfig_publicroom", true);
//設置為永久房間
answerForm.setAnswer("muc#roomconfig_persistentroom", true);
//允許修改昵稱
answerForm.setAnswer("x-muc#roomconfig_canchangenick", true);
//允許用戶登錄注冊房間
answerForm.setAnswer("x-muc#roomconfig_registration", true);
muc.sendConfigurationForm(answerForm);
muc.join(userName);
//關閉連接
closeConnection(connection);
return roomId;
}catch (Exception e){
e.printStackTrace();
}
return roomId;
}
# 加入群聊 #
/***
*
* 方法名稱:joinMultiUserChat
* 概要說明:加入群聊
*
* @param userName
* @param password
* @param roomId
* @return void
*
* @author ZhangYH
* @date 2020/06/17 10:30:28
*/
public static void joinMultiUserChat(String userName, String password, String roomId) {
try {
XMPPTCPConnection connection = initOpenfireConnect();
connection.login(userName,password);
// 使用XMPPConnection創建一個MultiUserChat窗口
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
//獲取會議室信息
MultiUserChat muc = manager.getMultiUserChat(roomId+"@conference."+OPENFIRE_SERVICE_NAME);
// 聊天室服務將會決定要接受的歷史記錄數量
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(0);
//history.setSince(new Date());
// 用戶加入聊天室
muc.join(userName, password, history, System.currentTimeMillis());
System.out.println("群聊加入成功........");
//關閉連接
closeConnection(connection);
}catch (Exception e){
e.printStackTrace();
}
}
注:openfire的群聊機制是這樣的:建群的時候必須要設置群組信息持久化(answerForm.setAnswer(“muc\#roomconfig\_persistentroom”, true);),要不然過一段時間群聊會自動刪除的,還有就是添加成員進入群聊以后,當成員下線以后會自動將下線的成員踢出群聊。
總結
以上是生活随笔為你收集整理的java smack 例子_java+smack+openfire即时通讯Im(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql通用日志不打印_解决logba
- 下一篇: cx oracle 中文 u,cx\U