XMPP系列之Smack(4.1.3 )(四)創(chuàng)建聊天室

XMPP系列之Smack(4.1.3)(一)登錄服務(wù)器
XMPP系列之Smack(4.1.3)(二)獲取好友分組
XMPP系列之Smack(4.1.3 )(三)獲取已加入的聊天室列表
這天兒猶如太上老君的八卦爐又再次倒落人間似得,一個(gè)字熱,四個(gè)字熱得要命!話說(shuō)又過(guò)了好久沒(méi)寫(xiě)點(diǎn)東西了,想想也有點(diǎn)小慚愧,廢話不多說(shuō)了,開(kāi)始本篇的主題吧!

首先獲取服務(wù)器名val SERVICE_NAME = mMultiUserChatManager.serviceNames
目的是為了獲取MultiUserChat這個(gè)對(duì)象,這里獲取的其實(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對(duì)象,目的是為了創(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è)置聊天室是持久聊天室,即將要被保存下來(lái)
            submitForm.setAnswer("muc#roomconfig_persistentroom", true);
            // 房間僅對(duì)成員開(kāi)放
            submitForm.setAnswer("muc#roomconfig_membersonly", false);
            // 允許占有者邀請(qǐng)其他人
            submitForm.setAnswer("muc#roomconfig_allowinvites", true);
            // 能夠發(fā)現(xiàn)占有者真實(shí) JID 的角色
            // submitForm.setAnswer("muc#roomconfig_whois", "anyone");
            // 登錄房間對(duì)話
            submitForm.setAnswer("muc#roomconfig_enablelogging", true);
            // 僅允許注冊(cè)的昵稱(chēng)登錄
            submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
            // 允許使用者修改昵稱(chēng)
            submitForm.setAnswer("x-muc#roomconfig_canchangenick", true);
            // 允許用戶(hù)注冊(cè)房間
            submitForm.setAnswer("x-muc#roomconfig_registration", false);
            // 發(fā)送已完成的表單(有默認(rèn)值)到服務(wù)器來(lái)配置聊天室
            mUserChat.sendConfigurationForm(submitForm);
            return true;

        } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException
                | SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
        return false;
    }

里面的配置可以根據(jù)需求看著改;好了,不出意外的話聊天室就這么輕易的創(chuàng)建成功了,驚不驚喜、意不意外!對(duì)了,除了配置方法外,其他使用的是kotlin語(yǔ)言,這是2017Google I/O大會(huì)上宣布的正式的官方開(kāi)發(fā)語(yǔ)言,還沒(méi)有接觸kotlin的小伙伴可要加油了,贈(zèng)上學(xué)習(xí)地址
https://wangjiegulu.gitbooks.io/kotlin-for-android-developers-zh/
http://www.lxweimin.com/p/39b5bf5cf962
https://juejin.im/entry/592e93b144d90400645f16f8

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容