3
3
import com .bernd .model .Chat ;
4
4
import com .bernd .model .ChatMessage ;
5
5
import com .bernd .model .ChatRequest ;
6
+ import com .bernd .model .Status ;
7
+ import com .bernd .model .StatusMap ;
8
+ import com .bernd .model .UserStatus ;
6
9
import com .bernd .model .UsersMessage ;
7
10
import com .bernd .util .Auth ;
8
- import org .springframework .http .ResponseEntity ;
11
+ import com .bernd .util .Sender ;
12
+ import java .security .Principal ;
13
+ import java .util .List ;
14
+ import java .util .Map ;
9
15
import org .springframework .messaging .core .MessageSendingOperations ;
10
16
import org .springframework .messaging .handler .annotation .MessageMapping ;
11
17
import org .springframework .stereotype .Controller ;
12
18
import org .springframework .web .bind .annotation .GetMapping ;
13
19
import org .springframework .web .bind .annotation .PathVariable ;
14
20
import org .springframework .web .bind .annotation .ResponseBody ;
15
21
16
- import java .security .Principal ;
17
- import java .util .ArrayList ;
18
- import java .util .TreeSet ;
19
- import java .util .concurrent .atomic .AtomicInteger ;
20
-
21
22
@ Controller
22
23
public class ChatController {
23
24
25
+ private static final long SCAN_TIMEOUT = 120 * 1000 ;
26
+
24
27
private final Chats chats ;
25
28
private final MessageSendingOperations <String > operations ;
29
+ private final Sender sender ;
30
+ private final StatusMap statusMap = new StatusMap ();
31
+ private long lastScan ;
26
32
27
33
ChatController (
28
34
Chats chats ,
29
- MessageSendingOperations <String > operations ) {
35
+ MessageSendingOperations <String > operations ,
36
+ Sender sender ) {
30
37
this .chats = chats ;
31
38
this .operations = operations ;
39
+ this .sender = sender ;
32
40
}
33
41
34
42
@ ResponseBody
@@ -37,8 +45,8 @@ public Chat getChat(@PathVariable String id) {
37
45
return chats .get (id );
38
46
}
39
47
40
- @ MessageMapping ("/chat/send/ " )
41
- public ResponseEntity <?> sendChat (ChatRequest chatRequest , Principal principal ) {
48
+ @ MessageMapping ("/chat/send" )
49
+ public void sendChat (ChatRequest chatRequest , Principal principal ) {
42
50
String user = Auth .getPrincipal (principal );
43
51
Chat chat = chats .get (chatRequest .id ());
44
52
ChatMessage message = new ChatMessage (chat .counter ().getAndIncrement (), chatRequest .message (), user );
@@ -47,6 +55,25 @@ public ResponseEntity<?> sendChat(ChatRequest chatRequest, Principal principal)
47
55
operations .convertAndSend ("/topic/users/" + chat .id (), new UsersMessage (chat .users ()));
48
56
}
49
57
operations .convertAndSend ("/topic/chat/" + chat .id (), message );
50
- return ResponseEntity .ok ().build ();
58
+ }
59
+
60
+ @ MessageMapping ("/chat/status" )
61
+ public void updateStatus (Status status , Principal principal ) {
62
+ String user = Auth .getPrincipal (principal );
63
+ UserStatus old = statusMap .put (user , UserStatus .create (status .room ()));
64
+ if (lastScan + SCAN_TIMEOUT < System .currentTimeMillis ()) {
65
+ Map <String , List <String >> allRooms = statusMap .allRooms ();
66
+ for (Map .Entry <String , List <String >> e : allRooms .entrySet ()) {
67
+ String room = e .getKey ();
68
+ List <String > users = e .getValue ();
69
+ sender .sendUsers (room , users );
70
+ }
71
+ lastScan = System .currentTimeMillis ();
72
+ } else if (old == null ) {
73
+ sender .sendUsers (status .room (), statusMap .usersInRoom (status .room ()));
74
+ } else if (!old .room ().equals (status .room ())) {
75
+ sender .sendUsers (status .room (), statusMap .usersInRoom (status .room ()));
76
+ sender .sendUsers (status .room (), statusMap .usersInRoom (old .room ()));
77
+ }
51
78
}
52
79
}
0 commit comments