我尝试使用Java创建多用户聊天。我正在使用smack库。 这是我创建multiuserchat的代码:
MultiUserChat muc = new MultiUserChat(connection, "roomname@somehost");
muc.create("mynickname");
Form form = muc.getConfigurationForm();
Form submitForm = form.createAnswerForm();
submitForm.setAnswer("muc#roomconfig_roomname", "A nice formatted Room Name");
submitForm.setAnswer("muc#roomconfig_roomdesc", "The description. It should be longer.");
muc.sendConfigurationForm(submitForm);
muc.addMessageListener(mucMessageListener); // mucMessageListener is a PacketListener
然后,我尝试使用mucMessageListener捕获上面创建的这个房间发送的消息:
private PacketListener mucMessageListener = new PacketListener() {
public void processPacket(Packet packet) {
if (packet instanceof Message) {
Message message = (Message) packet;
// this is where I got the problem
}
}
}
由于其他部分(不是此多用户的所有者)收到的消息,他可以以某种方式获得上面这一行中设置的值:
submitForm.setAnswer("muc#roomconfig_roomname", "A nice formatted Room Name");
你知道,获得房间的JID对视图来说并不是很好。我希望我能有一个字符串,其值是"一个很好的格式化房间名称"。
我们怎么能得到它?
答案 0 :(得分:1)
您可以通过以下代码轻松获取名称等配置:
MultiUserChatManager mucManager = MultiUserChatManager.getInstanceFor(connection);
RoomInfo info = mucManager.getRoomInfo(room.getRoom());
现在你可以得到这样的信息:
String mucName = info.getName();
Boolean isPersistence = info.isPersistent();
等。
答案 1 :(得分:0)
XEP-45 6.4中描述了检索muc#roomconfig_romname
的值。 Smack提供了MultiUserChat.getRoomInfo()
方法来执行查询。
RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, "roomname@somehost.com")
String roomDescription = roomInfo.getDescription()
答案 2 :(得分:0)
如果要读取var的值,例如config
中的房间标题名称Form form = chat.getConfigurationForm();
String value = form.getField("muc#roomconfig_roomname").getValues().next();
然后用价值做任何你想要的东西..