Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...???

like image 643
Rahul Kalidindi Avatar asked Dec 28 '22 20:12

Rahul Kalidindi


1 Answers

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");
like image 88
Michal Strzalkowski Avatar answered Dec 30 '22 09:12

Michal Strzalkowski