首页 > 解决方案 > 我想使用 smack 接收所有用户消息(不在群聊中)

问题描述

我正在创建一个聊天应用程序,一对一聊天工作正常,但是当涉及到多用户聊天时,我无法收听他们的消息我正在使用 smack 来实现 XMPP 协议这是代码

服务器配置部分是

    public void serverCongig() throws XMPPException {
     
    LOGGER.info(String.format("Initializing connection to server %1$s port 
       %2$d", server, port));
    SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
    config = new ConnectionConfiguration(server, 
        port,PropertyReader.getPropertiesValue("server_domain"));
 //        config.setSASLAuthenticationEnabled(false);
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
     
    connection = new XMPPConnection(config);
    try
    {
       connection.connect();
        LOGGER.info("Connected: " + connection.isConnected()+" Service 
         name"+connection.getServiceName());
        chatManager = connection.getChatManager();
        messageListener = new MyMessageListener();
    }
    catch(Exception e)
    {
                    callThread();
     
        e.printStackTrace();
    }
      finally
    {
        callThread();
    }
   
     
}

消息监听部分是 class MyMessageListener implements MessageListener {

    @Override
    public void processMessage(Chat chat, Message message) {
        try
        {
        String from = message.getTo();
        String body = message.getBody();
        String toUser="";
        LOGGER.info(String.format("Received message '%1$s' from %2$s", body, from));
        String[] user=from.split("@");
        for(int i=0;i<user.length;i++)
         toUser=user[0];
        LOGGER.info("Receiver Phone number"+toUser); 
        SendMsgToWhatsapp create=new SendMsgToWhatsapp();
        try {
            if(!body.equalsIgnoreCase(null))
            {
              create.processWhatsappMessage(toUser, body);

            }
        } catch (Exception ex) {
            callThread();

// Logger.getLogger(XmppManager.class.getName()).log(Level.SEVERE, null, ex); } } 捕捉(异常 e){ e.printStackTrace(); 调用线程();} }

标签: javaxmppopenfiresmack

解决方案


我有一个类似的问题,这就是我发现的:XEP-0313: Message Archive Management ( https://xmpp.org/extensions/xep-0313.html ) 描述了使用消息存档。

在 smack 中你可以这样使用它

  MamManager mamManager = MamManager.getInstanceFor(connection);
        MamManager.MamQueryArgs mamQueryArgs = MamManager.MamQueryArgs.builder()
                .limitResultsToJid(jid)
                .setResultPageSizeTo(10)
                .queryLastPage()
                .build();
        MamManager.MamQuery mamQuery = mamManager.queryArchive(mamQueryArgs);
        List<Message> messages = mamQuery.getMessages();

        for (Message m : messages) {
            System.out.println(m.getBody());
        }

但是当我尝试使用它时,我收到了 maven 依赖项的问题,所以我从 dailybuilds 手动加载了 smack jar ( https://download.igniterealtime.org/smack/dailybuilds/smack-SNAPSHOT-2020-07-21.zip ) 以及通过 maven 添加的剩余依赖项

就我而言,它有效。

https://download.igniterealtime.org/smack/docs/latest/javadoc/org/jivesoftware/smackx/mam/package-summary.html (smackx.mam javadoc)


推荐阅读