【问题标题】:How to know who received a message in a MUC room如何知道谁在 MUC 房间收到了消息
【发布时间】:2016-08-26 11:21:59
【问题描述】:

对于我的论文,我正在使用 Smack 记录一个使用 MUC 模块的 XMPP 网络。

另一个软件目前正在将物联网传感器数据发送到不同的 MUC 房间。

对于发送到 MUC 房间的每条消息,我想知道在发送消息时该房间中有哪些用户。这可能吗?我可以对每个 muc 房间使用 messageListener,但是侦听器只接收一条消息作为参数。因此,我无法知道在侦听器方法中谁登录了房间。

【问题讨论】:

    标签: xmpp smack multiuserchat


    【解决方案1】:

    您可以在 xmpp 的 StanzaListener 中获取所有 muc 消息。请按照几个步骤完成此操作

    步骤 1. 声明为全局变量

    ChatManagerListener chatListener;
    Chat chat;
    StanzaListener packetListener;
    

    第 2 步。在 oncreate 或片段中使用此代码

    注意:确保您已连接聊天服务器。

        packetListener = new StanzaListener() {
            @Override
            public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
    
                if (packet instanceof Message) {
                    final Message message = (Message) packet;
    
                   }
            }
        };
    
            XMPP.getInstance().getConnection(acitiviy)).addAsyncStanzaListener(stanzaListener, null);
    
        ServiceDiscoveryManager sdm = ServiceDiscoveryManager
            .getInstanceFor(XMPP.getInstance().getConnection(acitiviy)));
        sdm.addFeature("jabber.org/protocol/si");
        sdm.addFeature("http://jabber.org/protocol/si");
        sdm.addFeature("http://jabber.org/protocol/disco#info");
        sdm.addFeature("jabber:iq:privacy");
    

    步骤 3. 一对一聊天对象的方法

    void sendMessage(String message) {
    if (chat != null) {
        try {
            chat.sendMessage(message);
                Message msg = new Message();
                msg.setTo(JidCreate.bareFrom(jid));
                msg.setFrom(XMPP.getInstance().getConnection(acitiviy)
                        .getUser());
    
                ChatStateExtension ext = new ChatStateExtension(
                        ChatState.paused);
                msg.addExtension(ext);
                lastComposing = System.currentTimeMillis();
                chat.sendMessage(msg);
    
        } catch (SmackException.NotConnectedException e) {
        } catch (Exception e) {
    
        }
    }
    

    }

    第 4 步。关于销毁 XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener);

    希望这会对您有所帮助,如果您想了解更多信息,请查看here。谢谢你

    【讨论】:

      【解决方案2】:

      没有什么可以阻止您从侦听器中调用 Multi UserCaht.getParticipants()。但请注意:如果您的目标是确定其他接收者的接收者,那么这种方法是脆弱的。我还建议考虑在您的 IoT 用例中使用 PubSub 而不是 MUC。

      【讨论】:

      • 嗨,流!谢谢您的回答。如果房间是在同一个应用程序中通过 Smack 创建的,我只能调用这个 getParticipants() 方法,对吗?但是,房间是由外部实例创建的。
      猜你喜欢
      • 1970-01-01
      • 2018-03-22
      • 2018-10-18
      • 2021-01-04
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多