用asmack,服务器是tigase ,创建房间,加入聊天室,聊天等的基本功能
2016-12-07 11:24
232 查看
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; }
相关文章推荐
- 基于tigase服务器的安卓客户端开发---创建聊天
- smack在tigase服务器下创建聊天室
- 写了个Android聊天客户端框架,基本聊天功能、数据库、服务器都有。大家可以看一看。已经开源
- 简易聊天程序教程(二)服务器的基本功能
- Android聊天客户端Demo,开源了.基本功能都有,数据库,服务器都Ok
- Webix学习笔记-创建一个基本应用程序-05-和服务器交互之Create
- Android基于XMPP Smack openfire 开发的聊天室(六) 【加入房间、权限错误】
- 服务器负载均衡的基本功能和实现原理 推荐
- 封装了手机与服务器的数据交互的基本功能类
- Webix学习笔记-创建一个基本应用程序-03-和服务器交互之Retrieve
- 【Java Socket】【Flex Socket】【Java 多线程】实现基本聊天功能demo
- Android基于XMPP Smack openfire 开发的聊天室(四) 【创建房间、表单;报文】
- sqlserver2005新增功能:服务器级别触发器的创建(除了insert,update,delete)-引用
- MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能...
- MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能...
- Openfire MultiUserChat 多用户聊天 ---创建聊天室
- 第一次尝试搭建lcs服务器,以实现其基本功能
- 自己构造的即时通讯服务器基本功能搭建完成!
- JAVA多服务器通讯框架-聊天功能演示程序 V0.1 alpha 发布并提供下载
- Openfire MultiUserChat 多用户聊天 ---创建聊天室