|
1 | 1 | package cmf.commitField.global.websocket; |
2 | 2 |
|
| 3 | +import cmf.commitField.domain.noti.noti.dto.NotiDto; |
| 4 | +import cmf.commitField.domain.noti.noti.entity.Noti; |
| 5 | +import cmf.commitField.domain.noti.noti.service.NotiService; |
| 6 | +import cmf.commitField.domain.user.entity.User; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import lombok.RequiredArgsConstructor; |
3 | 9 | import lombok.extern.slf4j.Slf4j; |
4 | 10 | import org.springframework.stereotype.Component; |
5 | 11 | import org.springframework.web.socket.*; |
6 | 12 |
|
7 | 13 | import java.io.IOException; |
8 | | -import java.util.ArrayList; |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.util.HashMap; |
9 | 16 | import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
10 | 19 |
|
11 | 20 | @Component |
| 21 | +@RequiredArgsConstructor |
12 | 22 | @Slf4j |
13 | 23 | public class NotiWebSocketHandler implements WebSocketHandler { |
14 | | - |
15 | | - private final List<WebSocketSession> sessions = new ArrayList<>(); |
| 24 | + private final NotiService notiService; |
| 25 | + private final ObjectMapper objectMapper; |
| 26 | + private final Map<Long, WebSocketSession> sessions = new ConcurrentHashMap<>(); |
16 | 27 |
|
17 | 28 | @Override |
18 | 29 | public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
19 | | - sessions.add(session); |
20 | | - log.info("알림 WebSocket 연결됨: " + session); |
| 30 | + log.info("클라이언트 접속: {}", session.getId()); |
| 31 | + |
| 32 | + // 연결 성공 메시지 전송 |
| 33 | + Map<String, Object> connectMessage = new HashMap<>(); |
| 34 | + connectMessage.put("type", "SYSTEM"); |
| 35 | + connectMessage.put("connect", "알림 서버에 연결되었습니다."); |
| 36 | + connectMessage.put("timestamp", LocalDateTime.now().toString()); |
| 37 | + |
| 38 | + try { |
| 39 | + session.sendMessage(new TextMessage(objectMapper.writeValueAsString(connectMessage))); |
| 40 | + } catch (Exception e) { |
| 41 | + log.error("연결 메시지 전송 실패: {}", e.getMessage()); |
| 42 | + } |
21 | 43 | } |
22 | 44 |
|
23 | 45 | @Override |
24 | 46 | public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { |
25 | | - // 알림 메시지 처리 로직 (필요 시 구현) |
| 47 | + if (message instanceof TextMessage) { |
| 48 | + String payload = ((TextMessage) message).getPayload(); |
| 49 | + log.info("Received message: {}", payload); |
| 50 | + } else { |
| 51 | + log.warn("Received unsupported message type: {}", message.getClass().getSimpleName()); |
| 52 | + } |
26 | 53 | } |
27 | 54 |
|
28 | 55 | @Override |
29 | 56 | public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { |
30 | | - log.error("알림 WebSocket 오류: " + exception.getMessage()); |
| 57 | + log.error("WebSocket error: ", exception); |
| 58 | + session.close(CloseStatus.SERVER_ERROR); |
31 | 59 | } |
32 | 60 |
|
33 | 61 | @Override |
34 | | - public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { |
35 | | - sessions.remove(session); |
36 | | - log.info("알림 WebSocket 연결 종료됨: " + session); |
| 62 | + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
| 63 | + sessions.values().remove(session); |
| 64 | + log.info("WebSocket disconnected: {}", status); |
37 | 65 | } |
38 | 66 |
|
39 | 67 | @Override |
40 | 68 | public boolean supportsPartialMessages() { |
41 | 69 | return false; |
42 | 70 | } |
43 | 71 |
|
44 | | - // 모든 유저에게 알림 메시지 전송 |
45 | | - public void sendNotificationToAllUsers(String message) { |
46 | | - for (WebSocketSession session : sessions) { |
| 72 | + public void sendNotification(User receiver, List<NotiDto> noti) { |
| 73 | + WebSocketSession session = sessions.get(receiver.getId()); |
| 74 | + if (session != null && session.isOpen()) { |
47 | 75 | try { |
48 | | - session.sendMessage(new TextMessage(message)); |
| 76 | + String payload = objectMapper.writeValueAsString(noti); |
| 77 | + session.sendMessage(new TextMessage(payload)); |
49 | 78 | } catch (IOException e) { |
50 | | - log.error("알림 메시지 전송 실패: " + e.getMessage()); |
| 79 | + log.error("Failed to send WebSocket notification", e); |
51 | 80 | } |
52 | 81 | } |
53 | 82 | } |
|
0 commit comments