This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
7 changed files
with
194 additions
and
1 deletion.
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
20 changes: 20 additions & 0 deletions
20
...do-server/src/main/java/com/example/crescendoserver/domain/chat/config/WebSockConfig.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 com.example.crescendoserver.domain.chat.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.WebSocketHandler; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
@EnableWebSocket | ||
public class WebSockConfig implements WebSocketConfigurer { | ||
private final WebSocketHandler webSocketHandler; | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(webSocketHandler, "/ws/chat").setAllowedOrigins("*"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...rver/src/main/java/com/example/crescendoserver/domain/chat/controller/ChatController.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 com.example.crescendoserver.domain.chat.controller; | ||
|
||
import com.example.crescendoserver.domain.chat.room.ChatRoom; | ||
import com.example.crescendoserver.domain.chat.service.ChatService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/chat") | ||
public class ChatController { | ||
private final ChatService chatService; | ||
|
||
@PostMapping | ||
public ChatRoom createRoom(@RequestParam String name) { | ||
return chatService.createRoom(name); | ||
} | ||
|
||
@GetMapping | ||
public List<ChatRoom> findAllRoom() { | ||
return chatService.findAllRoom(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...rver/src/main/java/com/example/crescendoserver/domain/chat/hadler/WebSockChatHandler.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 com.example.crescendoserver.domain.chat.hadler; | ||
|
||
import com.example.crescendoserver.domain.chat.message.ChatMessage; | ||
import com.example.crescendoserver.domain.chat.room.ChatRoom; | ||
import com.example.crescendoserver.domain.chat.service.ChatService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.handler.TextWebSocketHandler; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class WebSockChatHandler extends TextWebSocketHandler { | ||
private final ObjectMapper objectMapper; | ||
private final ChatService chatService; | ||
|
||
@Override | ||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { | ||
String payload = message.getPayload(); | ||
log.info("payload {}", payload); | ||
ChatMessage chatMessage = objectMapper.readValue(payload, ChatMessage.class); | ||
ChatRoom room = chatService.findRoomById(chatMessage.getRoomId()); | ||
room.handleActions(session, chatMessage, chatService); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ndo-server/src/main/java/com/example/crescendoserver/domain/chat/message/ChatMessage.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,23 @@ | ||
package com.example.crescendoserver.domain.chat.message; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Builder | ||
@Getter | ||
@Setter | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class ChatMessage { | ||
// 메시지 타입 : 입장, 채팅 | ||
public enum MessageType { | ||
ENTER, TALK | ||
} | ||
private MessageType type; // 메시지 타입 | ||
private String roomId; // 방번호 | ||
private String sender; // 메시지 보낸사람 | ||
private String message; // 메시지 | ||
} |
35 changes: 35 additions & 0 deletions
35
crescendo-server/src/main/java/com/example/crescendoserver/domain/chat/room/ChatRoom.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 com.example.crescendoserver.domain.chat.room; | ||
|
||
import com.example.crescendoserver.domain.chat.message.ChatMessage; | ||
import com.example.crescendoserver.domain.chat.service.ChatService; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Getter | ||
public class ChatRoom { | ||
private String roomId; | ||
private String name; | ||
private Set<WebSocketSession> sessions = new HashSet<>(); | ||
|
||
@Builder | ||
public ChatRoom(String roomId, String name) { | ||
this.roomId = roomId; | ||
this.name = name; | ||
} | ||
|
||
public void handleActions(WebSocketSession session, ChatMessage chatMessage, ChatService chatService) { | ||
if (chatMessage.getType().equals(ChatMessage.MessageType.ENTER)) { | ||
sessions.add(session); | ||
chatMessage.setMessage(chatMessage.getSender() + "님이 입장했습니다."); | ||
} | ||
sendMessage(chatMessage, chatService); | ||
} | ||
|
||
public <T> void sendMessage(T message, ChatService chatService) { | ||
sessions.parallelStream().forEach(session -> chatService.sendMessage(session, message)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...ndo-server/src/main/java/com/example/crescendoserver/domain/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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.crescendoserver.domain.chat.service; | ||
|
||
import com.example.crescendoserver.domain.chat.room.ChatRoom; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class ChatService { | ||
|
||
private final ObjectMapper objectMapper; | ||
private Map<String, ChatRoom> chatRooms; | ||
|
||
@PostConstruct | ||
private void init() { | ||
chatRooms = new LinkedHashMap<>(); | ||
} | ||
|
||
public List<ChatRoom> findAllRoom() { | ||
return new ArrayList<>(chatRooms.values()); | ||
} | ||
|
||
public ChatRoom findRoomById(String roomId) { | ||
return chatRooms.get(roomId); | ||
} | ||
|
||
public ChatRoom createRoom(String name) { | ||
String randomId = UUID.randomUUID().toString(); | ||
ChatRoom chatRoom = ChatRoom.builder() | ||
.roomId(randomId) | ||
.name(name) | ||
.build(); | ||
chatRooms.put(randomId, chatRoom); | ||
return chatRoom; | ||
} | ||
|
||
public <T> void sendMessage(WebSocketSession session, T message) { | ||
try { | ||
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message))); | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
} | ||
} |