用asmack,服务器是tigase ,创建房间,加入聊天室,聊天等的基本功能

 
 
1.
  /**
     * 创建房间
     *
     * @param roomName 房间名称
     */
    public MultiUserChat createRoom(String roomName, String password) {
        MultiUserChat muc = null;
        try {
            if (getConnection() == null)
                return null;
            // TODO 完成建群操作
            // 创建一个 MultiUserChat对象  这里的"@muc."可以自己写
            muc = new MultiUserChat(getConnection(), getFullRoomname(roomName));
            muc.create(roomName);
            // 获得聊天室的配置表单
            Form form = muc.getConfigurationForm();
            // 根据原始表单创建一个要提交的新表单。
            Form submitForm = form.createAnswerForm();
            // 向要提交的表单添加默认答复
            for (Iterator<FormField> 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_moderatedroom", false);
            // 设置聊天室是持久聊天室,即将要被保存下来
            submitForm.setAnswer("muc#roomconfig_persistentroom", true);
            // 房间仅对成员开放
            submitForm.setAnswer("muc#roomconfig_membersonly", false);
            if (password == null || password.length() == 0) {
                // 不需要密码能进入的房间
                submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", false);
            } else {
                // 需要密码才能进入的房间
                submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
                // 设置房间密码
                submitForm.setAnswer("muc#roomconfig_roomsecret", password);
            }
//            // 允许使用者修改昵称
            submitForm.setAnswer("x-muc#roomconfig_canchangenick", true);
            // 登录房间对话
            submitForm.setAnswer("muc#roomconfig_enablelogging", true);
            muc.sendConfigurationForm(submitForm);
        } catch (XMPPException e) {
            Log.e("you wenti", "网络不给力,请重试" + e.getMessage());
            e.printStackTrace();
            return null;
        }

        return muc;
    }
 
 
 
2.  /**
     * 加入有密码的会议室
     *
     * @param user      昵称
       * @param roomsName 会议室名
     */
    public int joinPassMultiUserChat(String user, String password, String roomsName) {
        try {
            if (getConnection() == null)
                return 0;
            // 使用XMPPConnection创建一个MultiUserChat窗口
            if (mulChat != null) {
                mulChat.leave();
                mulChat = null;
            }

            mulChat = new MultiUserChat(getConnection(), roomsName + "@muc." + getConnection().getServiceName());
            // 聊天室服务将会决定要接受的历史记录数量
            DiscussionHistory history = new DiscussionHistory();
            history.setMaxChars(0);
            history.setSeconds(300);
//            history.setSince(new Date());

            // 用户加入聊天室
            mulChat.join(user, password, history, SmackConfiguration.getPacketReplyTimeout());
//            mulChat.changeNickname(user);//修改昵称
            Log.e("muc", "会议室【" + roomsName + "】加入成功........");
            return 1;

        } catch (Exception e) {
            e.printStackTrace();
//            if(Constants.IS_DEBUG)
            Log.e("muc", "会议室【" + roomsName + "】加入失败........");
            return 2;

        } finally {
        }
    }
 
3.* 查询会议室成员名字
 * @param muc */
public static List<String> findMulitUser(MultiUserChat muc){  
  List<String> listUser = new ArrayList<String>();  
  Iterator<String> it = muc.getOccupants();  
  //遍历出聊天室人员名称   
 while (it.hasNext()) {      
  // 聊天室成员名字       
 String name = StringUtils.parseResource(it.next());   
     listUser.add(name);   
 }    
return listUser;}
 
    /**
     * TODO 发送消息
     *
     * @param msg      消息内容
     * @param chatType 消息类型(聊天,还是群聊)
     */
    @SuppressLint("NewApi")
    public void sendMsg(String msg, int chatType) throws Exception {
        if (getConnection() == null) {
            throw new Exception("XmppException");
        }
        if (msg.isEmpty()) {
            Toast.makeText(context, "信息不能为空", Toast.LENGTH_SHORT).show();
        } else {
            //判断是  组聊  还是单聊
            if (chatType == 2) {
                //发送群聊信息,这里的mulChat,就是你加入房间时的muc
             mulChat.sendMessage(msg);
            } else if (chatType == 1) {
//                newchat.sendMessage(msg);
            }
        }
    }
 
 
    //根据roomJID获取聊天室成员数量 
,这里的mulChat,就是你加入房间时的muc
    public int  getRoomOccupantsCount() {
         int occupantsCount=   mulChat.getOccupantsCount();
        return occupantsCount;


    }

    //根据roomJID获取聊天室信息 这里的mulChat,就是你加入房间时的muc    
public RoomInfo getRoomInfo(String roomname) {
        RoomInfo roomInfo = null;
        try {
            roomInfo = MultiUserChat.getRoomInfo(getConnection(), getFullRoomname(roomname));
        } catch (XMPPException e) {
            e.printStackTrace();
        }
//        System.out.println(roomInfo.getRoom() + "-" + roomInfo.getSubject() + "-" + roomInfo.getOccupantsCount());
        return roomInfo;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值