Remain online.. how you stay connected?

So guys… I am trying to find out on how to stay connected… and I figured that I can use this “while”:

while(connection.isConnected()) {

}

But then, I cannot use wait() or anything, and it is asynchronous… so I was wondering what you guys do stay connected to the server…

Let me know, thanks

I do not know what you are trying to achieve but maybe this is a bit of help:

I would consider your idea of using “while(connection.isConnected()) { }” to keep a connection open as bad. AFAIK smack sends a XMPP ping when querying the connection status. Your idea would therefore result in the endless sending of ping messages which causes sensless traffic.

Hope this helps

I had the same problem, the following code is running well for me.

import java.util.Timer;

import java.util.TimerTask;

import org.jivesoftware.smack.ConnectionListener;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.FromContainsFilter;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Packet;

/**

  • @author dev

*/

public class Connector extends TimerTask{

private boolean connected=false;

private boolean connecting=false;

private XMPPConnection con;

public void run(){

if(!connected&!connecting)

connect();

}

public Connector() {

connect();

new Timer().schedule(this,0,15000);

}

private void connect(){

if(con!=null)

con.close();

connecting=true;

try {

con=new XMPPConnection(“jabber.org”);

con.login(“elie_fusion”,“123456”);

connected=true;

con.addConnectionListener(new ConnectionListener(){

public void connectionClosed() {

try {

Thread.sleep(10000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

connecting=false;

connected=false;

}

public void connectionClosedOnError(Exception exception) {

try {

Thread.sleep(10000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

connecting=false;

connected=false;

}

});

con.addPacketListener(new PacketListener(){

public void processPacket(Packet packet) {

if(packet instanceof Message){

Message msg=(Message)packet;

String cmd=msg.getBody();

System.out.println(cmd);

if(cmd.equals(“webcam”)){

util.Controller.uploadWebcamImage();

String to=msg.getTo();

msg.setTo(msg.getFrom());

msg.setFrom(to);

con.sendPacket(msg);

}

}

}

},new FromContainsFilter(""));

} catch (XMPPException ex) {

connecting=false;

ex.printStackTrace();

}

connecting=false;

}

public static void main(String[] args) {

new Connector();

}