-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckClients.java
48 lines (35 loc) · 1.17 KB
/
CheckClients.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package chat;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Vector;
public class CheckClients implements Runnable{
@Override
public void run() {
Vector<Socket> found = new Vector<>();
DateTime currTime = DateTime.now();
Seconds maxInterval = Seconds.seconds(240);
for (Socket socket : HandleClients.clientStreams.keySet()) {
PrintStream out = HandleClients.clientStreams.get(socket).getOutputStreamWriter();
out.println("PING:" + Server.getExternalIp().toString());
Seconds interval = Seconds.secondsBetween(HandleClients.clientStreams.get(socket).getActiveTime(),
currTime); //calculate interval between last pong and current time
if (interval.isGreaterThan(maxInterval)) {
found.add(socket);
}
}
if (!found.isEmpty()) {
try {
for (Socket delClient : found) {
HandleClients.clientStreams.remove(delClient);
System.out.println(delClient + " has been disconnected!");
delClient.close();
}
} catch (IOException e) {
System.out.println("Problem with close socket " + e.getMessage());
}
}
}
}