【问题标题】:Smack - How to read a MultiUserChat's configuration?Smack - 如何读取 MultiUserChat 的配置?
【发布时间】:2014-06-29 23:29:48
【问题描述】:

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

我们怎样才能得到它?

【问题讨论】:

    标签: java xmpp smack multiuserchat


    【解决方案1】:

    您可以通过以下代码轻松获取其名称等配置:

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

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

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

    等等。

    【讨论】:

    • 我正在使用 smack 和 openfire 在我的应用程序中使用此代码。亲爱的@Nilax,你能告诉我你的错误是什么吗?
    • 我的做法一模一样。 muc.create(Resourcepart.from(nickName));表单 form = muc.getConfigurationForm();表单 submitForm = form.createAnswerForm(); submitForm.setAnswer("muc#roomconfig_persistentroom", true); submitForm.setAnswer("muc#roomconfig_membersonly", true); submitForm.setAnswer("muc#roomconfig_roomdesc", room_desc); muc.sendConfigurationForm(submitForm);您能否建议我在设置和获取详细信息时哪里出错?
    • 您创建 MUC 的代码很好,如果您确定创建了 muc 并且您使用 multiUserChat.invite(...) 邀请了某个人加入它,那么我的代码必须在invitationReceived() 方法中工作已经使用 mMUCManager.addInvitationListener(...) 添加的
    【解决方案2】:

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

    RoomInfo roomInfo = MultiUserChat.getRoomInfo(connection, "roomname@somehost.com")
    String roomDescription = roomInfo.getDescription()
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2014-09-08
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        相关资源
        最近更新 更多