-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from mujik-tigers/feat/22-web-socket-enviromnent
Feat: 웹 소켓 환경 구축
- Loading branch information
Showing
16 changed files
with
269 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/site/youtogether/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package site.youtogether.config; | ||
|
||
import static site.youtogether.util.AppConstants.*; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.youtogether.util.interceptor.StompHandshakeInterceptor; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
@RequiredArgsConstructor | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
private final StompHandshakeInterceptor stompHandshakeInterceptor; | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
registry.enableSimpleBroker("/sub"); | ||
registry.setApplicationDestinationPrefixes("/pub"); | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint(STOMP_ENDPOINT).setAllowedOriginPatterns("http://localhost:3000", "https://you-together-web.vercel.app") | ||
.addInterceptors(stompHandshakeInterceptor) | ||
.withSockJS(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/site/youtogether/exception/user/UserNoExistenceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package site.youtogether.exception.user; | ||
|
||
import site.youtogether.exception.CustomException; | ||
import site.youtogether.exception.ErrorType; | ||
|
||
public class UserNoExistenceException extends CustomException { | ||
|
||
public UserNoExistenceException() { | ||
super(ErrorType.USER_NO_EXISTENCE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package site.youtogether.message; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
public class ChatMessage { | ||
|
||
private final MessageType messageType = MessageType.CHAT; | ||
|
||
private String roomCode; | ||
private Long userId; | ||
private String nickname; | ||
private String content; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package site.youtogether.message; | ||
|
||
public enum MessageType { | ||
|
||
CHAT, PARTICIPANTS_INFO, ROOM_TITLE | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/site/youtogether/message/application/RedisPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package site.youtogether.message.application; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.youtogether.message.ChatMessage; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisPublisher { | ||
|
||
private final ChannelTopic chatChannelTopic; | ||
private final RedisTemplate<String, String> redisTemplate; | ||
private final ObjectMapper objectMapper; | ||
|
||
public void publishMessage(ChatMessage chatMessage) { | ||
try { | ||
redisTemplate.convertAndSend(chatChannelTopic.getTopic(), objectMapper.writeValueAsString(chatMessage)); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/site/youtogether/message/application/RedisSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package site.youtogether.message.application; | ||
|
||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.youtogether.message.ChatMessage; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisSubscriber { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final SimpMessageSendingOperations messagingTemplate; | ||
|
||
public void sendMessage(String publishMessage) { | ||
try { | ||
ChatMessage chatMessage = objectMapper.readValue(publishMessage, ChatMessage.class); | ||
messagingTemplate.convertAndSend("/sub/messages/rooms/" + chatMessage.getRoomCode(), chatMessage); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/site/youtogether/message/presentation/MessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package site.youtogether.message.presentation; | ||
|
||
import static site.youtogether.util.AppConstants.*; | ||
|
||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.youtogether.exception.user.UserNoExistenceException; | ||
import site.youtogether.message.ChatMessage; | ||
import site.youtogether.message.application.RedisPublisher; | ||
import site.youtogether.user.User; | ||
import site.youtogether.user.infrastructure.UserStorage; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class MessageController { | ||
|
||
private final UserStorage userStorage; | ||
private final RedisPublisher redisPublisher; | ||
|
||
@MessageMapping("/messages") | ||
public void handleMessage(ChatMessage chatMessage, SimpMessageHeaderAccessor headerAccessor) { | ||
Long userId = (Long)headerAccessor.getSessionAttributes().get(USER_ID); | ||
User user = userStorage.findById(userId) | ||
.orElseThrow(UserNoExistenceException::new); | ||
|
||
chatMessage.setUserId(user.getUserId()); | ||
chatMessage.setNickname(user.getNickname()); | ||
|
||
redisPublisher.publishMessage(chatMessage); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/site/youtogether/util/interceptor/StompHandshakeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package site.youtogether.util.interceptor; | ||
|
||
import static site.youtogether.util.AppConstants.*; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.WebSocketHandler; | ||
import org.springframework.web.socket.server.HandshakeInterceptor; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import site.youtogether.config.property.CookieProperties; | ||
import site.youtogether.exception.cookie.CookieInvalidException; | ||
import site.youtogether.exception.cookie.CookieNoExistenceException; | ||
import site.youtogether.user.infrastructure.UserTrackingStorage; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class StompHandshakeInterceptor implements HandshakeInterceptor { | ||
|
||
private final CookieProperties cookieProperties; | ||
private final UserTrackingStorage userTrackingStorage; | ||
|
||
@Override | ||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, | ||
Map<String, Object> attributes) throws Exception { | ||
String[] cookies = request.getHeaders().get(HttpHeaders.COOKIE).get(0).split("; "); | ||
String cookieValue = Stream.of(cookies) | ||
.filter(cookie -> cookie.startsWith(cookieProperties.getName())) | ||
.map(cookie -> cookie.substring(cookie.indexOf("=") + 1)) | ||
.findAny() | ||
.orElseThrow(CookieNoExistenceException::new); | ||
|
||
Long userId = userTrackingStorage.findByCookieValue(cookieValue) | ||
.orElseThrow(CookieInvalidException::new); | ||
attributes.put(USER_ID, userId); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.