Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Sent user info
Browse files Browse the repository at this point in the history
  • Loading branch information
plamentotev committed Oct 22, 2023
1 parent 3acf64b commit 010eeac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +28,7 @@
public class CustomWebSocketHandler implements WebSocketHandler {

private final Map<String, WebSocketSession> activeSessions = new ConcurrentHashMap<>();
private final Map<String, String> sessionToProfileId = new ConcurrentHashMap<>();
private final Map<String, String> profileIdToSessionsId = new ConcurrentHashMap<>();

@Autowired
private LeaderBoardService leaderBoardService;
Expand All @@ -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
Expand All @@ -62,28 +61,35 @@ 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<LeaderBoard> response = new CustomWebSocketResponse<LeaderBoard>();
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 {
CustomWebSocketResponse<Profile> response = new CustomWebSocketResponse<Profile>();
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 010eeac

Please sign in to comment.