Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.goorm.team9.icontact.config.websocket;

import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;

@Component
public class WebSocketConnectListener implements ApplicationListener<SessionConnectedEvent> {

@Autowired
private WebSocketSessionService webSocketSessionService;

@Override
public void onApplicationEvent(SessionConnectedEvent event) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());

Long roomId = Long.parseLong(headerAccessor.getFirstNativeHeader("roomId"));
Long clientId = Long.parseLong(headerAccessor.getFirstNativeHeader("clientId"));
String nickname = headerAccessor.getFirstNativeHeader("senderNickname");

webSocketSessionService.addStompSession(roomId, clientId, nickname);
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.goorm.team9.icontact.config.websocket;

import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class WebSocketHandler extends TextWebSocketHandler {

private final WebSocketSessionService webSocketSessionService;

public WebSocketHandler(WebSocketSessionService webSocketSessionService) {
this.webSocketSessionService = webSocketSessionService;
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
Long roomId = (Long) session.getAttributes().get("roomId");
Long clientId = (Long) session.getAttributes().get("clientId");
String senderNickname = (String) session.getAttributes().get("senderNickname");
webSocketSessionService.addSession(roomId, clientId, senderNickname, session);
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
Long roomId = (Long) session.getAttributes().get("roomId");
String senderNickname = (String) session.getAttributes().get("senderNickname");
webSocketSessionService.removeSession(roomId, senderNickname);
}

}

//package com.goorm.team9.icontact.config.websocket;
//
//import com.goorm.team9.icontact.domain.chat.service.WebSocketSessionService;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//@Component
//public class WebSocketHandler extends TextWebSocketHandler {
//
// private final WebSocketSessionService webSocketSessionService;
//
// public WebSocketHandler(WebSocketSessionService webSocketSessionService) {
// this.webSocketSessionService = webSocketSessionService;
// }
//
// @Override
// public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// Long roomId = (Long) session.getAttributes().get("roomId");
// Long clientId = (Long) session.getAttributes().get("clientId");
// String senderNickname = (String) session.getAttributes().get("senderNickname");
// webSocketSessionService.addSession(roomId, clientId, senderNickname, session);
// }
//
// @Override
// public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// Long roomId = (Long) session.getAttributes().get("roomId");
// String senderNickname = (String) session.getAttributes().get("senderNickname");
// webSocketSessionService.removeSession(roomId, senderNickname);
// }
//
//}
//
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class WebSocketSessionService {
private final ChatRoomService chatRoomService;

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

public WebSocketSessionService(SimpMessagingTemplate messagingTemplate, ChatRoomService chatRoomService) {
this.messagingTemplate = messagingTemplate;
Expand Down Expand Up @@ -67,8 +68,15 @@ public boolean isUserOnline(String nickname) {
}

public boolean isUserInRoom(Long roomId, String nickname) {
return chatRoomSessions.getOrDefault(roomId, Map.of())
boolean webSocketConnected = chatRoomSessions
.getOrDefault(roomId, Map.of())
.containsKey(nickname);

boolean stompConnected = stompSessionMap
.getOrDefault(roomId, Map.of())
.getOrDefault(nickname, false);

return webSocketConnected || stompConnected;
}

public void sendPrivateMessage(String nickname, String destination, Object payload) {
Expand All @@ -80,4 +88,10 @@ public void sendPrivateMessage(String nickname, String destination, Object paylo
});
}

public void addStompSession(Long roomId, Long clientId, String nickname) {
stompSessionMap
.computeIfAbsent(roomId, k -> new ConcurrentHashMap<>())
.put(nickname, true);
}

}