我试图用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对视图来说并不是很好。我希望我可以有一个字符串,其值是“一个很好的格式化的房间名称”。
我们怎么能这样做?
您可以从此代码轻松获取名称等配置:
MultiUserChatManager mucManager = MultiUserChatManager.getInstanceFor(connection);
RoomInfo info = mucManager.getRoomInfo(room.getRoom());
现在你可以得到这样的信息:
String mucName = info.getName();
Boolean isPersistence = info.isPersistent();
等等。
在muc#roomconfig_romname
中描述了检索XEP-45 6.4的值。 Smack提供MultiUserChat.getRoomInfo()
方法来执行查询。
RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, "[email protected]")
String roomDescription = roomInfo.getDescription()
如果要读取var的值,例如config中的房间标题名称
Form form = chat.getConfigurationForm();
String value = form.getField("muc#roomconfig_roomname").getValues().next();
然后做有价值的任何你想要的东西..