3

我试图用 Java 创建一个多用户聊天。我正在使用 smack 库。这是我创建多用户聊天的代码:

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 对视图来说并不是很好。我希望我可以有一个值为“格式良好的房间名称”的字符串。

我们怎样才能得到它?

4

3 回答 3

1

您可以从此代码轻松获取名称等配置:

MultiUserChatManager mucManager = MultiUserChatManager.getInstanceFor(connection);
RoomInfo info = mucManager.getRoomInfo(room.getRoom());

现在您可以像这样获取它的信息:

String mucName = info.getName();
Boolean isPersistence = info.isPersistent();

等等。

于 2018-02-28T15:56:13.643 回答
0

XEP-45 6.4muc#roomconfig_romname中描述了检索 的值。Smack 提供了执行查询的方法。MultiUserChat.getRoomInfo()

RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, "roomname@somehost.com")
String roomDescription = roomInfo.getDescription()
于 2014-05-13T06:46:57.820 回答
0

如果您想读取 var 的值,例如配置中房间的标题名称

Form form = chat.getConfigurationForm();
String value =  form.getField("muc#roomconfig_roomname").getValues().next();

然后做任何你想做的有价值的事情..

于 2016-02-19T21:49:40.917 回答