XMPP系列之Smack(4.1.3)(一)登錄服務器
XMPP系列之Smack(4.1.3)(二)獲取好友分組
XMPP系列之Smack(4.1.3 )(三)獲取已加入的聊天室列表
這天兒猶如太上老君的八卦爐又再次倒落人間似得,一個字熱,四個字熱得要命!話說又過了好久沒寫點東西了,想想也有點小慚愧,廢話不多說了,開始本篇的主題吧!
首先獲取服務器名val SERVICE_NAME = mMultiUserChatManager.serviceNames
目的是為了獲取MultiUserChat
這個對象,這里獲取的其實是一個數組,然后取第一個元素(也就是我們前面搭建的服務器域名):
val userChat = mMultiUserChatManager.getMultiUserChat(roomName + "@" + SERVICE_NAME[0])
拿到userChat
后調用一次create(String nickname)
方法:
userChat.create(mUserVo.name)
到這里你肯定以為這就完了,no,no,no,還要進行最后一步:聊天室配置---->>>
created = XMPPConnectionManager.getInstance().configChatRoom(userChat)
這里的created
是一個Boolean對象,目的是為了創建成功回調后刷新列表,下面是配置單--->>>
/**
* 配置創建的聊天室信息
*
* @param mUserChat
*/
public boolean configChatRoom(MultiUserChat mUserChat) {
try {
Form form = mUserChat.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> fieldList = form.getFields();
for (int i = 0; i < fieldList.size(); i++) {
FormField field = fieldList.get(i);
if (!FormField.Type.hidden.equals(field.getType())
&& field.getVariable() != null) {
// 設置默認值作為答復
submitForm.setDefaultAnswer(field.getVariable());
}
}
// 設置聊天室是持久聊天室,即將要被保存下來
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
// 房間僅對成員開放
submitForm.setAnswer("muc#roomconfig_membersonly", false);
// 允許占有者邀請其他人
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
// 能夠發現占有者真實 JID 的角色
// submitForm.setAnswer("muc#roomconfig_whois", "anyone");
// 登錄房間對話
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
// 僅允許注冊的昵稱登錄
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
// 允許使用者修改昵稱
submitForm.setAnswer("x-muc#roomconfig_canchangenick", true);
// 允許用戶注冊房間
submitForm.setAnswer("x-muc#roomconfig_registration", false);
// 發送已完成的表單(有默認值)到服務器來配置聊天室
mUserChat.sendConfigurationForm(submitForm);
return true;
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException
| SmackException.NotConnectedException e) {
e.printStackTrace();
}
return false;
}
里面的配置可以根據需求看著改;好了,不出意外的話聊天室就這么輕易的創建成功了,驚不驚喜、意不意外!對了,除了配置方法外,其他使用的是kotlin語言,這是2017Google I/O大會上宣布的正式的官方開發語言,還沒有接觸kotlin的小伙伴可要加油了,贈上學習地址
https://wangjiegulu.gitbooks.io/kotlin-for-android-developers-zh/
http://www.lxweimin.com/p/39b5bf5cf962
https://juejin.im/entry/592e93b144d90400645f16f8