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