smack+openfire 群聊(三)(解决创建群失败的问题,以及永久房间问题)

本文介绍如何使用Smack和Openfire创建群聊,包括解决创建群失败的问题以及设置永久房间。首先,通过API创建房间并添加成员;然后,详细阐述发送群消息的步骤。实验前提是在Openfire中已创建管理员用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇文章,我们已经写了,单聊和注册用户,我们这节讲一下创建群聊的问题以及发送群消息

第一步,创建房间,以及房间成员加入


	public String createChatRoom(String nickName, String source, String kf) throws SmackException, IOException {
		HashMap<String, Object> map = new HashMap<>();
		map.put("customer_id", nickName);
		List<ChatGroup> list = chatGroupService.selectByMap(map);
		// 组装群聊jid,这里需要注意一下,群jid的格式就是 群名称@conference.openfire服务器名称
		String roomName = System.currentTimeMillis() + "";
		if (list.size() > 0) {
			return roomName;
		}

		// 管理员在每一个群里
		AbstractXMPPConnection connection = getXmpptcpConnection("admin", "admin");
		try {
			connection.connect();
			connection.login();
			ConnectionMap.put("admin", connection);
			String jid = roomName + "@conference." + connection.getServiceName();
			EntityBareJid groupJid = JidCreate.entityBareFrom(jid);

			MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
			MultiUserChat muc = manager.getMultiUserChat(groupJid);
			muc.create(Resourcepart.from("admin"));
			// 获得聊天室的配置表单
			Form subform = muc.getConfigurationForm();
			// 根据原始表单创建一个要提交的新表单。
			Form form = subform.createAnswerForm();
			// 设置聊天室的新拥有者
			List<String> owners = new ArrayList<>();
			owners.add("admin" + "@" + openfireServer);//admin加入群成员
			owners.add(nickName + "@" + openfireServer);//客户加入群成员
	
			// 指派客服逻辑-----------------------------------------------------
			EntityBareJid userJid = JidCreate.entityBareFrom(kf + "@" + openfireServer);//客服加入群成员
			owners.add(userJid + "");
		

			form.setAnswer("muc#roomconfig_passwordprotectedroom", false);
			form.setAnswer("muc#roomconfig_roomname", roomName);
			form.setAnswer("muc#roomconfig_persistentroom", true);
			form.setAnswer("muc#roomconfig_changesubject", true);
			form.setAnswer("muc#roomconfig_publicroom", true);
			form.setAnswer("muc#roomconfig_allowinvites", true);
			form.setAnswer("muc#roomconfig_membersonly", false);
			form.setAnswer("muc#roomconfig_moderatedroom", false);
			form.setAnswer("muc#roomconfig_roomowners", owners);
//			muc.sendConfigurationForm(new Form(DataForm.Type.submit));
	        muc.sendConfigurationForm(form);
			muc.join(Resourcepart.from("admin"));


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return roomName;

	}

第二步,就是发送群消息了

@RequestMapping("/sendRoomMsg")
	public void sendChatGroupMessage(String roomid, String username, String msg) throws Exception {
		AbstractXMPPConnection connection = ConnectionMap.get("admin");
	
				connection = getXmpptcpConnection("admin", "admin");
//		
			// 拼凑jid
			connection.connect();
			if (!connection.isAuthenticated()) {
				connection.login();
				ConnectionMap.put("admin", connection);
			}
		}
		EntityBareJid jid = JidCreate.entityBareFrom(roomid + "@conference." + connection.getServiceName());
		// 创建一个MultiUserChat
		MultiUserChat muc = MultiUserChatManager.getInstanceFor(connection).getMultiUserChat(jid);
		MucEnterConfiguration.Builder builder = muc.getEnterConfigurationBuilder(Resourcepart.from("admin"));
		// 只获取最后99条历史记录
		builder.requestNoHistory();
		builder.requestMaxCharsHistory(0);

		MucEnterConfiguration mucEnterConfiguration = builder.build();
//		// 加入群
		muc.join(mucEnterConfiguration);
		muc.sendMessage(msg);

//		connection.disconnect();

	}

注意,上面的admin是我已经在openfire里创建好的用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值