开发者

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.开发者_如何学CparseBareAddress(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...???


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");


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.


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.


Make sure you always check for the conflict error when you reconnect. This will happen when another client logs in with the same full JID (local@domain/resource) as the current session. You'll get this:

<stream:error>
    <conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
</stream:error>

Never autoreconnect when that was the error, or you will have written the dreaded "dueling resources" bug. Again. Like every other XMPP client author ever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜