How to receive Incoming XMPP Messages using Smack?

前端 未结 4 616
面向向阳花
面向向阳花 2020-12-13 09:44

I read some examples and tested them but all of them need to start a chat with someone first to receive Incoming Messages... I want to retrieve this Incoming Messages withou

相关标签:
4条回答
  • 2020-12-13 10:27

    You need to register a ChatListener to be notified of new chats, then you can add a message listener to them like normal:

    connection.getChatManager().addChatListener(new ChatManagerListenerImpl());
    
    ....
    
    private class ChatManagerListenerImpl implements ChatManagerListener {
    
        /** {@inheritDoc} */
        @Override
        public void chatCreated(final Chat chat, final boolean createdLocally) {
            chat.addMessageListener(...);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 10:28

    Please find the following code.
    Please add smack.jar & smackx.jar to your build path

    import org.jivesoftware.smack.ConnectionConfiguration;
    import org.jivesoftware.smack.PacketCollector;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.filter.AndFilter;
    import org.jivesoftware.smack.filter.PacketFilter;
    import org.jivesoftware.smack.filter.PacketTypeFilter;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Packet;
    
    public class GoogleTalkDemo extends Thread{
        private XMPPConnection xmppConnection;
    
        public void connect(String server, int port, String s) throws Exception {
            xmppConnection = new XMPPConnection(new ConnectionConfiguration(server, port,s));
            xmppConnection.connect();
        }
    
        public void disconnect(){
            if(xmppConnection != null){
                xmppConnection.disconnect();
                interrupt();
            }
        }
    
        public void login(String username, String password) throws Exception{
            connect("talk.google.com", 5222, "gmail.com");
            xmppConnection.login(username, password);
        }
    
        public void run(){
            try {
                login("youtID@sample.com", "your password");
                System.out.println("Login successful");
                listeningForMessages();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public static void main(String args[]) throws Exception {
            GoogleTalkDemo gtd = new GoogleTalkDemo();
            gtd.run();
        }
    
        public void listeningForMessages() {
            PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class));
            PacketCollector collector = xmppConnection.createPacketCollector(filter);
            while (true) {
                Packet packet = collector.nextResult();
                if (packet instanceof Message) {
                    Message message = (Message) packet;
                    if (message != null && message.getBody() != null)
                        System.out.println("Received message from "
                                + packet.getFrom() + " : "
                                + (message != null ? message.getBody() : "NULL"));
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 10:28
    private MultiUserChat   muc; /* Initialize muc */
    
    private void listeningForMessages() 
        {
            muc.addMessageListener(new PacketListener() {
                public void processPacket(Packet packet) 
                {
                    final Message message = (Message) packet;
    
                        // Do your action with the message              
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-13 10:40

    i just wanted to add a copy & paste sample:

      // connect to server
      XMPPConnection connection = new XMPPConnection("jabber.org");
      connection.connect();
      connection.login("user", "password"); // TODO: change user and pass
    
      // register listeners
      ChatManager chatmanager = connection.getChatManager();
      connection.getChatManager().addChatListener(new ChatManagerListener()
      {
        public void chatCreated(final Chat chat, final boolean createdLocally)
        {
          chat.addMessageListener(new MessageListener()
          {
            public void processMessage(Chat chat, Message message)
            {
              System.out.println("Received message: " 
                + (message != null ? message.getBody() : "NULL"));
            }
          });
        }
      });
    
      // idle for 20 seconds
      final long start = System.nanoTime();
      while ((System.nanoTime() - start) / 1000000 < 20000) // do for 20 seconds
      {
        Thread.sleep(500);
      }
      connection.disconnect();
    

    This sample connects to jabber.org and displays every received message on the console.

    0 讨论(0)
提交回复
热议问题