-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionManager.java
More file actions
47 lines (39 loc) · 1.12 KB
/
Copy pathSessionManager.java
File metadata and controls
47 lines (39 loc) · 1.12 KB
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
package network.chat.server;
import util.MyLogger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static util.MyLogger.log;
public class SessionManager {
private List<Session> sessions = new ArrayList<>();
public synchronized void add(Session session){
sessions.add(session);
}
public synchronized void remove(Session session){
sessions.remove(session);
}
public synchronized void closeAll(){
for (Session session : sessions) {
session.close();
}
sessions.clear();
}
public synchronized void sendAll(String message) {
for (Session session : sessions) {
try{
session.send(message);
} catch (IOException e){
log(e);
}
}
}
public synchronized List<String> getAllUsername() {
List<String> usernames = new ArrayList<>();
for(Session session : sessions) {
if(session.getUsername() != null) {
usernames.add(session.getUsername());
}
}
return usernames;
}
}