Skip to content

Commit

Permalink
Merge pull request #18 from UMC6th-TeamC/chat
Browse files Browse the repository at this point in the history
Chat update
  • Loading branch information
chamm99 authored Jun 22, 2024
2 parents 0bc57b9 + 45689ba commit baf5f0d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
package com.umc.teamC.domain.chat.controller;


import com.umc.teamC.domain.chat.entity.Chat;
import com.umc.teamC.domain.chat.service.ChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequiredArgsConstructor
//@CrossOrigin("*")
public class ChatController {

private final ChatService chatService;

//메세지 보내기 컨트롤러
@MessageMapping("/chat.message")
@SendTo("/topic/public")
public Map<String, Object> sendMessage(Map<String, Object> message) {
// System.out.println(message);
// 메시지 보내기 컨트롤러
@MessageMapping("/chat.message/{chatRoomId}")
@SendTo("/topic/{chatRoomId}")
public Map<String, Object> sendMessage(
@PathVariable Long chatRoomId,
Map<String, Object> message) {
// 메시지 확인 로그
log.info("Received message: {}", message);
String sender = message.get("sender").toString();
String content = message.get("content").toString();

Chat message1 = Chat.builder()
.sender(sender)
.content(content)
.chatId(chatRoomId)
.build();

Chat chat = chatService.saveMessage(message1);
Map<String, Object> result = new HashMap<>();
result.put("id", chat.getChatId());
Expand All @@ -38,9 +46,10 @@ public Map<String, Object> sendMessage(Map<String, Object> message) {
return result;
}

@MessageMapping("/chat.addUser")
@SendTo("/topic/public")
public Chat addUser(Chat chat) {
@MessageMapping("/chat.addUser/{chatRoomId}")
@SendTo("/topic/{chatRoomId}")
public Chat addUser(@PathVariable Long chatRoomId, Chat chat) {
chat.setId(chatRoomId);
return chat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ChatRoomConverter {

public static ChatRoomDto.ChatRoomResponseDto toChatRoomResponseDto(ChatRoom chatRoom, String lastMessage, Integer memberCount) {
return ChatRoomDto.ChatRoomResponseDto.builder()
.chatRoomId(chatRoom.getRoomId())
.dateTime(chatRoom.getDateTime())
.region(chatRoom.getRegion())
.lastMessage(lastMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public record ChatRoomListResponseDto (

@Builder
public record ChatRoomResponseDto (
Long chatRoomId,
LocalDateTime dateTime,
String region,
String lastMessage,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/umc/teamC/domain/chat/entity/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public class Chat extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chat_room_id")
private ChatRoom chatRoom;

public void setId(Long id) {
chatId = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.umc.teamC.domain.chat.handler;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class WebsocketHandler extends TextWebSocketHandler {

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
System.out.println("Connected: " + session.getId());
}

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.println("Received: " + message.getPayload());
session.sendMessage(new TextMessage("Hello, client!"));
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
System.out.println("Disconnected: " + session.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

@Controller
@ResponseBody
//@CrossOrigin("*")
public class UserController {
private final UserService userService;

Expand Down

0 comments on commit baf5f0d

Please sign in to comment.