-
Notifications
You must be signed in to change notification settings - Fork 4
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 #135 from kakao-tech-campus-2nd-step3/develop
Develop
- Loading branch information
Showing
17 changed files
with
316 additions
and
159 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
93 changes: 44 additions & 49 deletions
93
src/main/java/com/helpmeCookies/chat/controller/WebSocketChatController.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 |
---|---|---|
@@ -1,90 +1,85 @@ | ||
package com.helpmeCookies.chat.controller; | ||
|
||
|
||
import com.helpmeCookies.chat.dto.ImageMessageDTO; | ||
import com.helpmeCookies.chat.dto.ChatMessageDto; | ||
import com.helpmeCookies.chat.entity.ChatMessage; | ||
import com.helpmeCookies.chat.entity.ChatRoom; | ||
import com.helpmeCookies.chat.entity.MessageType; | ||
import com.helpmeCookies.chat.service.ChatMessageService; | ||
import com.helpmeCookies.chat.service.ChatRoomService; | ||
import com.helpmeCookies.chat.util.ImageStorageUtil; | ||
import com.helpmeCookies.global.exception.user.UserNotFoundException; | ||
import com.helpmeCookies.user.entity.User; | ||
import com.helpmeCookies.user.service.UserService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.handler.annotation.DestinationVariable; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.messaging.simp.annotation.SubscribeMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/v1/websocket") | ||
public class WebSocketChatController { | ||
|
||
private final SimpMessageSendingOperations messagingTemplate; | ||
private final ChatMessageService chatMessageService; | ||
private final ChatRoomService chatRoomService; | ||
private final UserService userService; | ||
private final ImageStorageUtil imageStorageUtil; | ||
|
||
public WebSocketChatController(SimpMessageSendingOperations messagingTemplate, ChatMessageService chatMessageService, ChatRoomService chatRoomService, UserService userService, ImageStorageUtil imageStorageUtil) { | ||
|
||
public WebSocketChatController(SimpMessageSendingOperations messagingTemplate, | ||
ChatMessageService chatMessageService, | ||
ChatRoomService chatRoomService) { | ||
this.messagingTemplate = messagingTemplate; | ||
this.chatMessageService = chatMessageService; | ||
this.chatRoomService = chatRoomService; | ||
this.userService = userService; | ||
this.imageStorageUtil = imageStorageUtil; | ||
|
||
} | ||
|
||
@MessageMapping("/chat/{chatRoomId}") | ||
@Operation(summary = "채팅 메시지 전송", description = "WebSocket을 통해 특정 채팅방에 텍스트 메시지를 전송합니다.") | ||
@Parameter(name = "chatRoomId", description = "채팅방 ID", required = true, schema = @Schema(type = "long")) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "성공적으로 메시지를 전송했습니다."), | ||
@ApiResponse(responseCode = "404", description = "사용자를 찾을 수 없습니다.", content = @Content) | ||
}) | ||
public void chat(@DestinationVariable Long chatRoomId, | ||
@Parameter(description = "전송할 채팅 메시지", required = true) ChatMessage message) throws UserNotFoundException { | ||
ChatRoom chatRoom = chatRoomService.getChatRoomById(chatRoomId); | ||
User sender = userService.findByEmail(message.getSender().getEmail()) | ||
.orElseThrow(() -> new UserNotFoundException("발신자를 찾을 수 없습니다: " + message.getSender().getEmail())); | ||
public void chat(@DestinationVariable Long chatRoomId, @RequestBody ChatMessageDto messageDto) { | ||
ChatMessage savedMessage = chatMessageService.saveMessage(chatRoomId, messageDto); | ||
|
||
chatMessageService.saveMessage(chatRoom, sender, message.getContent()); | ||
messagingTemplate.convertAndSend("/api/sub/chat/rooms/" + chatRoomId, message); | ||
messagingTemplate.convertAndSend("/v1/sub/chat/rooms/" + chatRoomId, savedMessage); | ||
} | ||
|
||
@MessageMapping("/chat/{chatRoomId}/file") | ||
@Operation(summary = "파일 전송", description = "WebSocket을 통해 특정 채팅방에 파일을 전송합니다.") | ||
@Parameter(name = "chatRoomId", description = "채팅방 ID", required = true, schema = @Schema(type = "long")) | ||
@Parameter(name = "file", description = "전송할 파일", required = true, schema = @Schema(type = "string", format = "binary")) | ||
@Parameter(name = "userEmail", description = "발신자 이메일", required = true, schema = @Schema(type = "string")) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "성공적으로 파일을 전송했습니다."), | ||
@ApiResponse(responseCode = "404", description = "사용자를 찾을 수 없습니다.", content = @Content) | ||
}) | ||
public void sendFile(@DestinationVariable Long chatRoomId, | ||
@RequestParam("file") MultipartFile file, | ||
@RequestParam String userEmail) | ||
throws UserNotFoundException, IOException { | ||
ChatRoom chatRoom = chatRoomService.getChatRoomById(chatRoomId); | ||
User sender = userService.findByEmail(userEmail) | ||
.orElseThrow(() -> new UserNotFoundException("발신자를 찾을 수 없습니다: " + userEmail)); | ||
@RequestParam String fileBase64, | ||
@RequestParam String userEmail) { | ||
try { | ||
ChatMessage message = chatMessageService.saveFileMessage(chatRoomId, userEmail, fileBase64); | ||
|
||
ChatMessage message = chatMessageService.saveFileMessage(chatRoom, sender, file); | ||
String imagePath = message.getImageUrl(); | ||
byte[] imageBytes = java.nio.file.Files.readAllBytes(new File(imagePath).toPath()); | ||
byte[] imageBytes = chatMessageService.convertImageUrlToBytes(message.getContent()); | ||
String encodedContent = ChatMessageDto.getImageContent(imageBytes); | ||
|
||
ImageMessageDTO imageMessageDTO = new ImageMessageDTO(message, imageBytes); | ||
messagingTemplate.convertAndSend("/api/sub/chat/rooms/" + chatRoomId, imageMessageDTO); | ||
ChatMessageDto chatMessageDto = new ChatMessageDto( | ||
message.getChatRoom().getId(), | ||
message.getSender().getEmail(), | ||
encodedContent, | ||
message.getTimestamp().toString(), | ||
MessageType.IMAGE | ||
); | ||
|
||
messagingTemplate.convertAndSend("/api/sub/chat/rooms/" + chatRoomId, chatMessageDto); | ||
} catch (UserNotFoundException | IOException e) { | ||
|
||
log.error("파일 전송 중 오류 발생: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
|
||
@SubscribeMapping("/chat/rooms/{chatRoomId}/list") | ||
public List<ChatMessage> sendInitialMessages(@DestinationVariable Long chatRoomId) { | ||
ChatRoom chatRoom = chatRoomService.getChatRoomById(chatRoomId); | ||
|
||
return chatMessageService.getMessagesByChatRoom(chatRoom); | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/helpmeCookies/chat/dto/ChatMessageDto.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,38 @@ | ||
package com.helpmeCookies.chat.dto; | ||
|
||
import com.helpmeCookies.chat.entity.MessageType; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class ChatMessageDto { | ||
private Long chatRoomId; | ||
private String sender; | ||
private String content; | ||
private String timestamp; | ||
private MessageType messageType; | ||
|
||
public ChatMessageDto() { | ||
} | ||
|
||
public ChatMessageDto(Long chatRoomId, String sender, String content, String timestamp, MessageType messageType) { | ||
this.chatRoomId = chatRoomId; | ||
this.sender = sender; | ||
this.content = content; | ||
this.timestamp = timestamp; | ||
this.messageType = messageType; | ||
} | ||
|
||
public ChatMessageDto(String sender, String content, MessageType messageType) { | ||
this.sender = sender; | ||
this.content = content; | ||
this.messageType = messageType; | ||
} | ||
|
||
public static String getImageContent(byte[] contentBytes) { | ||
return "data:image/base64:," + java.util.Base64.getEncoder().encodeToString(contentBytes); | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.