您的位置:首页 > 运维架构

基于Openfire Smack开发即时通讯应用、获取所有群组、加入群聊、创建群组(四)

2017-03-22 17:25 429 查看

一:前面几篇博客分别介绍了环境搭建、注册登录。单聊 等等…..那么现在我们就来来看看怎么玩群聊。

基于Openfire Smack开发即时通讯应用、搭建Openfire服务器(一)

基于Openfire Smack开发即时通讯应用、Spark安装,注册,登录,退出登录(二)

基于Openfire Smack开发即时通讯应用、获取离线消息,发送消息,联系人列表,添加好友(三)

二:获取服务器上的所有群组

/**
* 获取服务器上的所有群组
*/
private List<HostedRoom> getHostedRoom() {
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
try {
//serviceNames->conference.106.14.20.176
List<String> serviceNames = manager.getServiceNames();
for (int i = 0; i < serviceNames.size(); i++) {
return manager.getHostedRooms(serviceNames.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


三:加入一个群聊

/**
* 加入一个群聊聊天室
*
* @param jid 聊天室ip 格式为>>群组名称@conference.ip
* @param nickName 用户在聊天室中的昵称
* @param password 聊天室密码 没有密码则传""
* @return
*/
public MultiUserChat join(String jid, String nickName, String password) {
try {
// 使用XMPPConnection创建一个MultiUserChat窗口
MultiUserChat muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(jid);
// 聊天室服务将会决定要接受的历史记录数量
DiscussionHistory history = new DiscussionHistory();
history.setMaxChars(0);
// 用户加入聊天室
muc.join(nickName, password);
return muc;
} catch (XMPPException | SmackException e) {
e.printStackTrace();
if ("XMPPError: not-authorized - auth".equals(e.getMessage())) {
//需要密码加入
}
return null;
}
}


四:监听这个群的所有会话消息

/**
* @param jid 格式为>>群组名称@conference.ip
*/
private void initListener(String jid) {
MultiUserChat multiUserChat = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(jid);
multiUserChat.addMessageListener(new MessageListener() {
@Override
public void processMessage(final Message message) {
//当消息返回为空的时候,表示用户正在聊天窗口编辑信息并未发出消息
if (!TextUtils.isEmpty(message.getBody())) {
//收到的消息
}
}
});
}


五:发送一条消息;这个也就一代码的事了

MultiUserChat multiUserChat = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(jid);
multiUserChat.sendMessage("Hello World");


六:创建一个群聊

/**
* 创建群聊聊天室
*
* @param roomName 聊天室名字
* @param nickName 创建者在聊天室中的昵称
* @param password 聊天室密码
* @return
*/
public MultiUserChat createChatRoom(String roomName, String nickName, String password) {
MultiUserChat muc;
try {
// 创建一个MultiUserChat
muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(roomName + "@conference." + connection.getServiceName());
// 创建聊天室
boolean isCreated = muc.createOrJoin(nickName);
if (isCreated) {
// 获得聊天室的配置表单
Form form = muc.getConfigurationForm();
// 根据原始表单创建一个要提交的新表单。
Form submitForm = form.createAnswerForm();
// 向要提交的表单添加默认答复
List<FormField> fields = form.getFields();
for (int i = 0; fields != null && i < fields.size(); i++) {
if (FormField.Type.hidden != fields.get(i).getType() &&
fields.get(i).getVariable() != null) {
// 设置默认值作为答复
submitForm.setDefaultAnswer(fields.get(i).getVariable());
}
}
// 设置聊天室的新拥有者
List owners = new ArrayList();
owners.add(connection.getUser());// 用户JID
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// 设置聊天室是持久聊天室,即将要被保存下来
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
// 房间仅对成员开放
submitForm.setAnswer("muc#roomconfig_membersonly", false);
// 允许占有者邀请其他人
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
if (password != null && password.length() != 0) {
// 进入是否需要密码
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
// 设置进入密码
submitForm.setAnswer("muc#roomconfig_roomsecret", password);
}
// 能够发现占有者真实 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", false);
// 允许用户注册房间
submitForm.setAnswer("x-muc#roomconfig_registration", false);
// 发送已完成的表单(有默认值)到服务器来配置聊天室
muc.sendConfigurationForm(submitForm);
Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "创建失败", Toast.LENGTH_SHORT).show();
}
} catch (XMPPException | SmackException e) {
e.printStackTrace();
Toast.makeText(this, "创建失败" + e.getMessage(), Toast.LENGTH_LONG).show();
return null;
}
return muc;
}


到这里差不多一些重要的Api就说完了,使用也是很easy的。源码地址

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐