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:
parent
37b56c9370
commit
14896e4d31
1 changed files with 27 additions and 2 deletions
|
@ -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) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue