forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
90 additions
and
143 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
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
2 changes: 1 addition & 1 deletion
2
...squad/chat/dto/ChatRoomCreateRequest.java → ...at/dto/request/ChatRoomCreateRequest.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,4 +1,4 @@ | ||
package kr.codesquad.chat.dto; | ||
package kr.codesquad.chat.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
|
20 changes: 20 additions & 0 deletions
20
be/src/main/java/kr/codesquad/chat/dto/request/SendMessageRequest.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,20 @@ | ||
package kr.codesquad.chat.dto.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class SendMessageRequest { | ||
private Long chatRoomId; | ||
private Long senderId; | ||
private String content; | ||
|
||
@Builder | ||
public SendMessageRequest(Long chatRoomId, Long senderId, String content) { | ||
this.chatRoomId = chatRoomId; | ||
this.senderId = senderId; | ||
this.content = content; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
be/src/main/java/kr/codesquad/chat/dto/response/ChatRoomCreateResponse.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,10 @@ | ||
package kr.codesquad.chat.dto.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ChatRoomCreateResponse { | ||
private final Long chatRoomId; | ||
} |
41 changes: 38 additions & 3 deletions
41
be/src/main/java/kr/codesquad/chat/service/ChatService.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,26 +1,61 @@ | ||
package kr.codesquad.chat.service; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import kr.codesquad.chat.dto.ChatMapper; | ||
import kr.codesquad.chat.dto.ChatRoomCreateRequest; | ||
import kr.codesquad.chat.dto.request.ChatRoomCreateRequest; | ||
import kr.codesquad.chat.dto.request.SendMessageRequest; | ||
import kr.codesquad.chat.dto.response.ChatRoomCreateResponse; | ||
import kr.codesquad.chat.repository.ChatMessageRepository; | ||
import kr.codesquad.chat.repository.ChatRoomRepository; | ||
import kr.codesquad.user.entity.User; | ||
import kr.codesquad.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class ChatService { | ||
|
||
private final ChatRoomRepository chatRoomRepository; | ||
private final ChatMessageRepository chatMessageRepository; | ||
private final UserRepository userRepository; | ||
private final ObjectMapper objectMapper; | ||
private final ConcurrentMap<Long, Set<WebSocketSession>> sessionMap = new ConcurrentHashMap<>(); | ||
|
||
public void createRoom(ChatRoomCreateRequest chatRoomCreateRequest, String loginId) { | ||
public ChatRoomCreateResponse createRoom(ChatRoomCreateRequest chatRoomCreateRequest, String loginId) { | ||
User user = userRepository.findByLoginId(loginId); | ||
chatRoomRepository.save(ChatMapper.INSTANCE.toChatRoom(chatRoomCreateRequest, user.getId())); | ||
return ChatMapper.INSTANCE.toChatRoomCreateResponse( | ||
chatRoomRepository.save(ChatMapper.INSTANCE.toChatRoom(chatRoomCreateRequest, user.getId()))); | ||
} | ||
|
||
public void sendMessage(Long chatRoomId, WebSocketSession session, SendMessageRequest message) { | ||
if (!sessionMap.containsKey(chatRoomId)) { | ||
sessionMap.put(chatRoomId, new HashSet<>()); | ||
} | ||
Set<WebSocketSession> sessions = sessionMap.get(chatRoomId); | ||
sessions.add(session); | ||
sessionMap.put(chatRoomId, sessions); | ||
sessions.parallelStream().forEach(s -> sendMessage(s, message)); | ||
} | ||
|
||
private <T> void sendMessage(WebSocketSession session, T message) { | ||
try { | ||
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
be/src/main/java/kr/codesquad/core/websocket/MsgController.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
be/src/main/java/kr/codesquad/core/websocket/MsgService.java
This file was deleted.
Oops, something went wrong.
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