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
24
25
private final Chats chats ;
25
26
private final MessageSendingOperations <String > operations ;
27
+ private final Sender sender ;
28
+ private final StatusMap statusMap = new StatusMap ();
29
+ private long lastScan ;
30
+ private static final long SCAN_TIMEOUT = 120 * 1000 ;
26
31
27
32
ChatController (
28
33
Chats chats ,
29
- MessageSendingOperations <String > operations ) {
34
+ MessageSendingOperations <String > operations ,
35
+ Sender sender ) {
30
36
this .chats = chats ;
31
37
this .operations = operations ;
38
+ this .sender = sender ;
32
39
}
33
40
34
41
@ ResponseBody
@@ -37,8 +44,8 @@ public Chat getChat(@PathVariable String id) {
37
44
return chats .get (id );
38
45
}
39
46
40
- @ MessageMapping ("/chat/send/ " )
41
- public ResponseEntity <?> sendChat (ChatRequest chatRequest , Principal principal ) {
47
+ @ MessageMapping ("/chat/send" )
48
+ public void sendChat (ChatRequest chatRequest , Principal principal ) {
42
49
String user = Auth .getPrincipal (principal );
43
50
Chat chat = chats .get (chatRequest .id ());
44
51
ChatMessage message = new ChatMessage (chat .counter ().getAndIncrement (), chatRequest .message (), user );
@@ -47,6 +54,25 @@ public ResponseEntity<?> sendChat(ChatRequest chatRequest, Principal principal)
47
54
operations .convertAndSend ("/topic/users/" + chat .id (), new UsersMessage (chat .users ()));
48
55
}
49
56
operations .convertAndSend ("/topic/chat/" + chat .id (), message );
50
- return ResponseEntity .ok ().build ();
57
+ }
58
+
59
+ @ MessageMapping ("/chat/status" )
60
+ public void updateStatus (Status status , Principal principal ) {
61
+ String user = Auth .getPrincipal (principal );
62
+ UserStatus old = statusMap .put (user , UserStatus .create (status .room ()));
63
+ if (lastScan + SCAN_TIMEOUT < System .currentTimeMillis ()) {
64
+ Map <String , List <String >> allRooms = statusMap .allRooms ();
65
+ for (Map .Entry <String , List <String >> e : allRooms .entrySet ()) {
66
+ String room = e .getKey ();
67
+ List <String > users = e .getValue ();
68
+ sender .sendUsers (room , users );
69
+ }
70
+ lastScan = System .currentTimeMillis ();
71
+ } else if (old == null ) {
72
+ sender .sendUsers (status .room (), statusMap .usersInRoom (status .room ()));
73
+ } else if (!old .room ().equals (status .room ())) {
74
+ sender .sendUsers (status .room (), statusMap .usersInRoom (status .room ()));
75
+ sender .sendUsers (status .room (), statusMap .usersInRoom (old .room ()));
76
+ }
51
77
}
52
78
}
0 commit comments