Skip to content

Commit 1a83184

Browse files
Merge pull request #150 from DeepDive-Final-Project/fix/websocket
[#149] 웹소캣 설정 수정
2 parents a477eec + a0be29d commit 1a83184

3 files changed

Lines changed: 76 additions & 35 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.goorm.team9.icontact.config.websocket;
2+
3+
import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.ApplicationListener;
6+
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.socket.messaging.SessionConnectedEvent;
9+
10+
@Component
11+
public class WebSocketConnectListener implements ApplicationListener<SessionConnectedEvent> {
12+
13+
@Autowired
14+
private WebSocketSessionService webSocketSessionService;
15+
16+
@Override
17+
public void onApplicationEvent(SessionConnectedEvent event) {
18+
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
19+
20+
Long roomId = Long.parseLong(headerAccessor.getFirstNativeHeader("roomId"));
21+
Long clientId = Long.parseLong(headerAccessor.getFirstNativeHeader("clientId"));
22+
String nickname = headerAccessor.getFirstNativeHeader("senderNickname");
23+
24+
webSocketSessionService.addStompSession(roomId, clientId, nickname);
25+
}
26+
27+
}
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
package com.goorm.team9.icontact.config.websocket;
2-
3-
import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
4-
import org.springframework.stereotype.Component;
5-
import org.springframework.web.socket.CloseStatus;
6-
import org.springframework.web.socket.WebSocketSession;
7-
import org.springframework.web.socket.handler.TextWebSocketHandler;
8-
9-
@Component
10-
public class WebSocketHandler extends TextWebSocketHandler {
11-
12-
private final WebSocketSessionService webSocketSessionService;
13-
14-
public WebSocketHandler(WebSocketSessionService webSocketSessionService) {
15-
this.webSocketSessionService = webSocketSessionService;
16-
}
17-
18-
@Override
19-
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
20-
Long roomId = (Long) session.getAttributes().get("roomId");
21-
Long clientId = (Long) session.getAttributes().get("clientId");
22-
String senderNickname = (String) session.getAttributes().get("senderNickname");
23-
webSocketSessionService.addSession(roomId, clientId, senderNickname, session);
24-
}
25-
26-
@Override
27-
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
28-
Long roomId = (Long) session.getAttributes().get("roomId");
29-
String senderNickname = (String) session.getAttributes().get("senderNickname");
30-
webSocketSessionService.removeSession(roomId, senderNickname);
31-
}
32-
33-
}
34-
1+
//package com.goorm.team9.icontact.config.websocket;
2+
//
3+
//import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
4+
//import org.springframework.stereotype.Component;
5+
//import org.springframework.web.socket.CloseStatus;
6+
//import org.springframework.web.socket.WebSocketSession;
7+
//import org.springframework.web.socket.handler.TextWebSocketHandler;
8+
//
9+
//@Component
10+
//public class WebSocketHandler extends TextWebSocketHandler {
11+
//
12+
// private final WebSocketSessionService webSocketSessionService;
13+
//
14+
// public WebSocketHandler(WebSocketSessionService webSocketSessionService) {
15+
// this.webSocketSessionService = webSocketSessionService;
16+
// }
17+
//
18+
// @Override
19+
// public void afterConnectionEstablished(WebSocketSession session) throws Exception {
20+
// Long roomId = (Long) session.getAttributes().get("roomId");
21+
// Long clientId = (Long) session.getAttributes().get("clientId");
22+
// String senderNickname = (String) session.getAttributes().get("senderNickname");
23+
// webSocketSessionService.addSession(roomId, clientId, senderNickname, session);
24+
// }
25+
//
26+
// @Override
27+
// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
28+
// Long roomId = (Long) session.getAttributes().get("roomId");
29+
// String senderNickname = (String) session.getAttributes().get("senderNickname");
30+
// webSocketSessionService.removeSession(roomId, senderNickname);
31+
// }
32+
//
33+
//}
34+
//

src/main/java/com/goorm/team9/icontact/domain/chat/service/WebSocketSessionService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class WebSocketSessionService {
1515
private final ChatRoomService chatRoomService;
1616

1717
private final Map<Long, Map<String, WebSocketSession>> chatRoomSessions = new ConcurrentHashMap<>();
18+
private final Map<Long, Map<String, Boolean>> stompSessionMap = new ConcurrentHashMap<>();
1819

1920
public WebSocketSessionService(SimpMessagingTemplate messagingTemplate, ChatRoomService chatRoomService) {
2021
this.messagingTemplate = messagingTemplate;
@@ -67,8 +68,15 @@ public boolean isUserOnline(String nickname) {
6768
}
6869

6970
public boolean isUserInRoom(Long roomId, String nickname) {
70-
return chatRoomSessions.getOrDefault(roomId, Map.of())
71+
boolean webSocketConnected = chatRoomSessions
72+
.getOrDefault(roomId, Map.of())
7173
.containsKey(nickname);
74+
75+
boolean stompConnected = stompSessionMap
76+
.getOrDefault(roomId, Map.of())
77+
.getOrDefault(nickname, false);
78+
79+
return webSocketConnected || stompConnected;
7280
}
7381

7482
public void sendPrivateMessage(String nickname, String destination, Object payload) {
@@ -80,4 +88,10 @@ public void sendPrivateMessage(String nickname, String destination, Object paylo
8088
});
8189
}
8290

91+
public void addStompSession(Long roomId, Long clientId, String nickname) {
92+
stompSessionMap
93+
.computeIfAbsent(roomId, k -> new ConcurrentHashMap<>())
94+
.put(nickname, true);
95+
}
96+
8397
}

0 commit comments

Comments
 (0)