XMPP系列之Smack(4.1.3)(一)登錄服務(wù)器
XMPP系列之Smack(4.1.3)(二)獲取好友分組
XMPP系列之Smack(4.1.3 )(三)獲取已加入的聊天室列表
這天兒猶如太上老君的八卦爐又再次倒落人間似得为流,一個(gè)字熱,四個(gè)字熱得要命让簿!話說又過了好久沒寫點(diǎn)東西了敬察,想想也有點(diǎn)小慚愧,廢話不多說了拜英,開始本篇的主題吧静汤!
首先獲取服務(wù)器名val SERVICE_NAME = mMultiUserChatManager.serviceNames
目的是為了獲取MultiUserChat
這個(gè)對象,這里獲取的其實(shí)是一個(gè)數(shù)組居凶,然后取第一個(gè)元素(也就是我們前面搭建的服務(wù)器域名):
val userChat = mMultiUserChatManager.getMultiUserChat(roomName + "@" + SERVICE_NAME[0])
拿到userChat
后調(diào)用一次create(String nickname)
方法:
userChat.create(mUserVo.name)
到這里你肯定以為這就完了虫给,no,no,no,還要進(jìn)行最后一步:聊天室配置---->>>
created = XMPPConnectionManager.getInstance().configChatRoom(userChat)
這里的created
是一個(gè)Boolean對象侠碧,目的是為了創(chuàng)建成功回調(diào)后刷新列表抹估,下面是配置單--->>>
/**
* 配置創(chuàng)建的聊天室信息
*
* @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) {
// 設(shè)置默認(rèn)值作為答復(fù)
submitForm.setDefaultAnswer(field.getVariable());
}
}
// 設(shè)置聊天室是持久聊天室,即將要被保存下來
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
// 房間僅對成員開放
submitForm.setAnswer("muc#roomconfig_membersonly", false);
// 允許占有者邀請其他人
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
// 能夠發(fā)現(xiàn)占有者真實(shí) 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);
// 發(fā)送已完成的表單(有默認(rèn)值)到服務(wù)器來配置聊天室
mUserChat.sendConfigurationForm(submitForm);
return true;
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException
| SmackException.NotConnectedException e) {
e.printStackTrace();
}
return false;
}
里面的配置可以根據(jù)需求看著改弄兜;好了药蜻,不出意外的話聊天室就這么輕易的創(chuàng)建成功了,驚不驚喜替饿、意不意外语泽!對了,除了配置方法外视卢,其他使用的是kotlin語言踱卵,這是2017Google I/O大會上宣布的正式的官方開發(fā)語言,還沒有接觸kotlin的小伙伴可要加油了据过,贈上學(xué)習(xí)地址
https://wangjiegulu.gitbooks.io/kotlin-for-android-developers-zh/
http://www.reibang.com/p/39b5bf5cf962
https://juejin.im/entry/592e93b144d90400645f16f8