How to check whether the client is connected to XMPP server or not

10,141

Solution 1

You should be able to accomplish this by using a ConnectionListener on your connection. This will allow you to detect when the connection is dropped and try to handle it appropriately.

The listed code should hardly ever hit the else condition since the connection would have had to have been open to receive the packet you are processing in the first place.

Solution 2

As suggested above, you need to attach a connection listener like so:

Connection connection = (Connection)params[0];
connection.connect();

connection.addPacketListener(new PacketListener() {

                                 @Override
                                 public void processPacket(Packet packet) {
                                     System.out.println("Received message");
                                 }
                             }, new PacketFilter() {

                                 @Override
                                 public boolean accept(Packet packet) {
                                     System.out.println("Received message");
                                     return true;
                                 }
                             });

connection.addConnectionListener(new ConnectionListener() {
    @Override
    public void connectionClosed() {
        System.out.println("uhoh");
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        System.out.println("uhoh");
    }

    @Override
    public void reconnectingIn(int i) {
        if(i < 4){
            //TODO notify
        }
    }

    @Override
    public void reconnectionSuccessful() {
        System.out.println("uhoh");
    }

    @Override
    public void reconnectionFailed(Exception e) {
        System.out.println("uhoh");
    }
});

connection.login("test2", "test");

Solution 3

I experienced this problem myself and now I have solved my problem and it's working quite right.

Here is the solution :-

  1. Go to your openfire server. Login it.
  2. Now after opening it, click on the SERVER Tab.
  3. After opening the SERVER TAB open the sub tab namely - SERVER SETTINGS
  4. After SERVER SETTINGS is opened, click on the SERVER TO SERVER Settings on the left side.
  5. Now here in this is the Second option - IDLE CONNECTION SETTINGS.
  6. Set it on - NEVER CLOSE IDLE CONNECTIONS.

Your problem will be resolved.

Share:
10,141
Rahul Kalidindi
Author by

Rahul Kalidindi

Solutions Architect and Dev Android iOS Mobile Apps Chatbots

Updated on June 07, 2022

Comments

  • Rahul Kalidindi
    Rahul Kalidindi about 2 years

    I am working an XMPP client for both Android and iPhone. I have been able to connect to the server and get messages too.

    But the Client is disconnecting with the XMPP server after few mins. The IOS XMPP Framework consists of a delegate method that indicates whether the client is connected to the server or not. If the connection is not disconnected then we can reconnect using the delegate method. I have used the following code in the Android client(I am using aSmack library) to check whether connection is present or not. But this seems to be not working.

    public void recieveMessage()
    {  
    
               Log.e("xmppclient","will receive messages");
               PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
               connection.addPacketListener(new PacketListener() {
    
               public void processPacket(Packet packet) {
                   if(connection != null && connection.isConnected()) 
                   {
                       Message message = (Message) packet;
                       if (message.getBody() != null)
                            {
                       fromName = StringUtils.parseBareAddress(message.getFrom());
                       String messageBody=message.getBody();
                       Log.e("Message", messageBody );
                       Intent i = new Intent(NEW_MESSAGE);  
                       i.putExtra("username", StringUtils.parseBareAddress(message.getFrom()));
                       i.putExtra("message", message.getBody());
                       sendBroadcast(i);
                            }
                       else
                       {
    
                           connectToXmpp();
                       }
                   }
    
                }
    
               }
       ,filter);
       }
    

    connectToXMPP() is the method that opens a new connection.

    Is there any other way to check the connection and reconnect to XMPP as soon as connection is gone...???

  • Mark Molina
    Mark Molina almost 11 years
    Could you explain this with an example please?
  • Michal Strzalkowski
    Michal Strzalkowski almost 11 years
    @MarkMolina Added example below.
  • Xairoo
    Xairoo almost 11 years
    Server to server settings have nothing to do with client to server connections.
  • Shailesh
    Shailesh almost 4 years
    I want to know that whenever XMPP connection is closed connectionClose() method is closed right?
  • Ninja
    Ninja almost 4 years