Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat :: chat
Browse files Browse the repository at this point in the history
  • Loading branch information
osoohynn committed Aug 26, 2024
1 parent 972981b commit 0020eea
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crescendo-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter:1.0.0-SNAPSHOT'

// 소켓 통신
// implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-websocket'

}

Expand Down
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("*");
}
}
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();
}
}
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);
}
}
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; // 메시지
}
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));
}
}
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);
}
}
}

0 comments on commit 0020eea

Please sign in to comment.