Skip to content

Commit

Permalink
fix: 종료시 connection close 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
david-parkk committed Sep 21, 2024
1 parent 4b5f1f7 commit b5eccf9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,29 @@ public class TagGameMatchingQueue {
private static final Random random = new Random();
private final TagGameUserService tagGameUserService;

public static void addPlayer(String userId, WebSocketSession webSocketSession){
if(!sessionMap.containsKey(userId)) {
queueing.offer(new AbstractMap.SimpleEntry<>(userId,webSocketSession));
sessionMap.put(userId,webSocketSession);
public static void addPlayer(String userId, WebSocketSession webSocketSession) {
if (sessionMap.containsKey(userId)) {
if(!sessionMap.get(userId).isOpen())
sessionMap.remove(userId);
}
if (!sessionMap.containsKey(userId)) {
queueing.offer(new AbstractMap.SimpleEntry<>(userId, webSocketSession));
sessionMap.put(userId, webSocketSession);
webSocketSession.send(Mono.just(webSocketSession.textMessage("매칭 대기 중 입니다."))).subscribe();
}

}

private static Map.Entry<String, WebSocketSession> getPlayer(){
private static Map.Entry<String, WebSocketSession> getPlayer() {
return queueing.poll();
}

public static int getQueuePlayers(){
public static int getQueuePlayers() {
return queueing.size();
}

private void createGameRoom(String roomId, List<Map.Entry<String, WebSocketSession>> players) {
TagGameRoom tagGameRoom = new TagGameRoom(roomId,players);
TagGameRoom tagGameRoom = new TagGameRoom(roomId, players);
TagGameRoomManager.addGameRoom(tagGameRoom);
}

Expand All @@ -56,15 +60,15 @@ private String confirmGameRoomName(List<Map.Entry<String, WebSocketSession>> pla

@Scheduled(fixedRate = 1000)
public void matchPlayers() throws InterruptedException {
while(queueing.size() >= 4){
while (queueing.size() >= 4) {
Map.Entry<String, WebSocketSession> player1 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player2 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player3 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player4 = TagGameMatchingQueue.getPlayer();

// 세션 값 확인
if(player1.getValue().isOpen() && player2.getValue().isOpen()
&& player3.getValue().isOpen() && player4.getValue().isOpen()){
if (player1.getValue().isOpen() && player2.getValue().isOpen()
&& player3.getValue().isOpen() && player4.getValue().isOpen()) {
List<Map.Entry<String, WebSocketSession>> players = List.of(player1, player2, player3, player4);

Map.Entry<String, WebSocketSession> tagger = selectTagger(players);
Expand All @@ -81,21 +85,20 @@ public void matchPlayers() throws InterruptedException {
webSocketSession.send(Mono.just(webSocketMessage)).subscribe();
}
Thread.sleep(5000);
tagGameUserService.initialize(players,tagger,integerList);
tagGameUserService.initialize(players, tagger, integerList);
createGameRoom(gameRoom, players);

}
else{
if(player1.getValue().isOpen()){
} else {
if (player1.getValue().isOpen()) {
MatchingQueue.requeue(player1);
}
if(player2.getValue().isOpen()){
if (player2.getValue().isOpen()) {
MatchingQueue.requeue(player2);
}
if(player3.getValue().isOpen()){
if (player3.getValue().isOpen()) {
MatchingQueue.requeue(player3);
}
if(player4.getValue().isOpen()){
if (player4.getValue().isOpen()) {
MatchingQueue.requeue(player4);
}
}
Expand All @@ -105,6 +108,7 @@ public void matchPlayers() throws InterruptedException {

/**
* 술래를 정한다
*
* @param players
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public TagGameRoom(String roomId, List<Map.Entry<String, WebSocketSession>> play
this.players = players;
}

public void delete(){
for (Map.Entry<String, WebSocketSession> player : players) {
player.getValue().close().subscribe();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static void addGameRoom(TagGameRoom tagGameRoom) {
public static TagGameRoom getGameRoom(String roomId) { return gameRooms.get(roomId); }

public static void deleteGameRoom(String roomId) {
gameRooms.remove(roomId); }
TagGameRoom tagGameRoom = gameRooms.get(roomId);
tagGameRoom.delete();
gameRooms.remove(roomId);
}


}

0 comments on commit b5eccf9

Please sign in to comment.