From 010eeac58c6bda6b2d4ec034aeb460135b6703f4 Mon Sep 17 00:00:00 2001 From: Plamen Totev Date: Sun, 22 Oct 2023 11:47:31 +0300 Subject: [PATCH] Sent user info --- .../handler/CustomWebSocketHandler.java | 24 ++++++++++++------- .../service/ProductService.java | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/mosquitobytes/carboncritters/handler/CustomWebSocketHandler.java b/backend/src/main/java/mosquitobytes/carboncritters/handler/CustomWebSocketHandler.java index 8e0401a..37a1cd6 100644 --- a/backend/src/main/java/mosquitobytes/carboncritters/handler/CustomWebSocketHandler.java +++ b/backend/src/main/java/mosquitobytes/carboncritters/handler/CustomWebSocketHandler.java @@ -19,7 +19,6 @@ import org.springframework.web.util.UriComponentsBuilder; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -29,7 +28,7 @@ public class CustomWebSocketHandler implements WebSocketHandler { private final Map activeSessions = new ConcurrentHashMap<>(); - private final Map sessionToProfileId = new ConcurrentHashMap<>(); + private final Map profileIdToSessionsId = new ConcurrentHashMap<>(); @Autowired private LeaderBoardService leaderBoardService; @@ -50,7 +49,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio // Send the user back to the client sendUserInfo(userId.get(0), session); - sendLeaderBoard(session, false); + sendLeaderBoard(session); } @Override @@ -62,20 +61,27 @@ public void handleMessage(WebSocketSession session, WebSocketMessage message) public void sendLeaderBoardToAllActiveSessions() throws IOException { for (WebSocketSession activeSession : this.activeSessions.values()) { - sendLeaderBoard(activeSession, true); + sendLeaderBoard(activeSession); } } - public void sendLeaderBoard(WebSocketSession session, boolean sendUserUpdate) throws IOException { + public void sendLeaderBoard(WebSocketSession session) throws IOException { CustomWebSocketResponse response = new CustomWebSocketResponse(); response.setType("ws/server/leaderBoard"); response.setPayload(this.leaderBoardService.getLeaderBoardUsers()); session.sendMessage(new TextMessage(convertToJson(response))); - if (sendUserUpdate) { - var userId = sessionToProfileId.get(session.getId()); - sendUserInfo(userId, session); + } + + public void sendUserInfo(String userId) throws IOException { + var sessionId = profileIdToSessionsId.get(userId); + if (sessionId == null) { + return; } + + var session = activeSessions.get(sessionId); + + sendUserInfo(userId, session); } private void sendUserInfo(String userId, WebSocketSession session) throws IOException { @@ -83,7 +89,7 @@ private void sendUserInfo(String userId, WebSocketSession session) throws IOExce response.setType("ws/server/user"); response.setPayload(this.profileService.getProfile(Long.parseLong(userId))); session.sendMessage(new TextMessage(convertToJson(response))); - sessionToProfileId.put(session.getId(), userId); + profileIdToSessionsId.put(userId, session.getId()); } private String convertToJson(CustomWebSocketResponse response) throws JsonProcessingException { diff --git a/backend/src/main/java/mosquitobytes/carboncritters/service/ProductService.java b/backend/src/main/java/mosquitobytes/carboncritters/service/ProductService.java index b480354..76461c8 100644 --- a/backend/src/main/java/mosquitobytes/carboncritters/service/ProductService.java +++ b/backend/src/main/java/mosquitobytes/carboncritters/service/ProductService.java @@ -39,6 +39,7 @@ public void addProduct(Long consumerId, String productCode) { try { webSocketHandler.sendLeaderBoardToAllActiveSessions(); + webSocketHandler.sendUserInfo(consumerId.toString()); } catch (IOException e) { throw new RuntimeException(e); }