1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-27 13:07:58 +01:00

Implemented keep-alives.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2219 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2004-01-28 21:30:37 +00:00 committed by mtucker
parent 37b56c9370
commit 14896e4d31

View file

@ -74,6 +74,7 @@ class PacketWriter {
private List listeners = new ArrayList();
private Thread listenerThread;
private LinkedList sentPackets = new LinkedList();
private Timer keepAliveTimer = new Timer(true);
/**
* Creates a new packet writer with the specified connection.
@ -100,6 +101,10 @@ class PacketWriter {
};
listenerThread.setName("Smack Writer Listener Processor");
listenerThread.setDaemon(true);
// Schedule a keep-alive task for once every 30 seconds. It will write
// out a space character each time it runs to keep the TCP/IP connection open.
keepAliveTimer.scheduleAtFixedRate(new KeepAliveTask(), 30000, 30000);
}
/**
@ -207,8 +212,10 @@ class PacketWriter {
while (!done) {
Packet packet = nextPacket();
if (packet != null) {
writer.write(packet.toXML());
writer.flush();
synchronized (writer) {
writer.write(packet.toXML());
writer.flush();
}
}
}
// Close the stream.
@ -220,6 +227,7 @@ class PacketWriter {
finally {
try {
writer.close();
keepAliveTimer.cancel();
}
catch (Exception e) { }
}
@ -310,4 +318,21 @@ class PacketWriter {
}
}
}
/**
* A TimerTask that keeps connections to the server alive by sending a space
* character. The
*/
private class KeepAliveTask extends TimerTask {
public void run() {
if (!done) {
synchronized (writer) {
try {
writer.write(" ");
}
catch (Exception e) { }
}
}
}
}
}