简体   繁体   中英

I want to receive all users message using smack(Not in Group Chat)

I am creating a chat application,one to one chatting is working properly,but when comes to multi user chat i am not able to listen their messages I am using smack for implementing the XMPP protocol Here is the code

Server configuration part is

    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();
    }
   
     
}

Message listening part is 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); } } catch(Exception e) { e.printStackTrace(); callThread(); } }

I had a similar question and that's what I found: XEP-0313: Message Archive Management ( https://xmpp.org/extensions/xep-0313.html ) describes to work with the message archive.

In smack you can use it like that

  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());
        }

But when I tried to use it, I received problems with dependencies in maven, so I loaded smack jar manually from dailybuilds ( https://download.igniterealtime.org/smack/dailybuilds/smack-SNAPSHOT-2020-07-21.zip ) and the remaining dependencies added through maven

In my case it work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM