Skip to content

Commit 4d478df

Browse files
committed
Feat: web socket 설정 수정
1 parent 9c941a7 commit 4d478df

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cmf.commitField.global.websocket;
22

3+
import cmf.commitField.global.error.ErrorCode;
4+
import cmf.commitField.global.exception.CustomException;
35
import lombok.extern.slf4j.Slf4j;
46
import org.springframework.stereotype.Component;
57
import org.springframework.web.socket.CloseStatus;
@@ -8,20 +10,28 @@
810
import org.springframework.web.socket.WebSocketSession;
911

1012
import java.io.IOException;
11-
import java.util.ArrayList;
12-
import java.util.List;
13+
import java.util.*;
1314

1415
@Component
1516
@Slf4j
1617
public class ChatWebSocketHandler implements WebSocketHandler {
1718

18-
private List<WebSocketSession> list = new ArrayList<>();
19+
private Map<Long, List<WebSocketSession>> chatRooms = new HashMap<>();
20+
// 방의 키값
21+
1922

2023
// 연결이 되었을 때
2124
@Override
2225
public void afterConnectionEstablished(WebSocketSession session)
2326
throws Exception {
24-
list.add(session);
27+
// list.add(session);
28+
Long roomId = extractRoomId(session);
29+
// roomId 가 없을 경우, session list (new ArrayList)
30+
List<WebSocketSession> roomSessions = chatRooms.getOrDefault(roomId, new ArrayList<>());
31+
// 세션 추가
32+
roomSessions.add(session);
33+
// 해당 방의 키값에 session list 추가
34+
chatRooms.put(roomId, roomSessions);
2535
log.info(session + "의 클라이언트 접속");
2636
}
2737

@@ -30,15 +40,22 @@ public void afterConnectionEstablished(WebSocketSession session)
3040
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
3141
throws Exception {
3242
// 메시지 처리 로직
33-
String payload = message.getPayload().toString();
34-
log.info("전송 메시지 : " + payload);
35-
// 받은 메시지 다른 client에게 전달
36-
for (WebSocketSession s : list) {
37-
try {
38-
s.sendMessage(message);
39-
} catch (IOException e) {
40-
e.printStackTrace();
43+
Long roomId = extractRoomId(session);
44+
List<WebSocketSession> roomSessions = chatRooms.get(roomId);
45+
if (roomSessions != null) {
46+
String payload = message.getPayload().toString();
47+
log.info("전송 메시지: " + payload);
48+
49+
for (WebSocketSession msg : roomSessions) {
50+
try {
51+
msg.sendMessage(message);
52+
} catch (IOException e) {
53+
throw new CustomException(ErrorCode.CHAT_ERROR);
54+
}
4155
}
56+
} else {
57+
log.info("해당 채팅방에 클라이언트가 없습니다.");
58+
throw new CustomException(ErrorCode.NOT_EXIST_CLIENT);
4259
}
4360
}
4461

@@ -53,7 +70,12 @@ public void handleTransportError(WebSocketSession session, Throwable exception)
5370
@Override
5471
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
5572
throws Exception {
56-
list.remove(session);
73+
Long roomId = extractRoomId(session); // 클라이언트가 속한 채팅방 ID를 추출
74+
75+
List<WebSocketSession> roomSessions = chatRooms.get(roomId);
76+
if (roomSessions != null) {
77+
roomSessions.remove(session);
78+
}
5779
log.info(session + "의 클라이언트 접속 해제");
5880
}
5981

@@ -63,4 +85,16 @@ public void afterConnectionClosed(WebSocketSession session, CloseStatus closeSta
6385
public boolean supportsPartialMessages() {
6486
return false;
6587
}
88+
89+
private Long extractRoomId(WebSocketSession session) {
90+
Long roomId = null;
91+
String uri = Objects.requireNonNull(session.getUri()).toString();
92+
String[] uriParts = uri.split("/");
93+
// EX_URL) /chat/room/{roomId} 일 때 roomId 추출
94+
// 늘어난다면 수 변경해주면.. (일단 임시로 설정)
95+
if (uriParts.length >= 3 && uriParts[2].equals("room")) {
96+
roomId = Long.valueOf(uriParts[3]);
97+
}
98+
return roomId;
99+
}
66100
}

0 commit comments

Comments
 (0)