Skip to content

Commit

Permalink
ChatController
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev committed Sep 15, 2024
1 parent c611214 commit 7000fbd
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package com.chat.yourway.controller.websocket;


import com.chat.yourway.dto.request.MessageRequestDto;
import com.chat.yourway.dto.response.MessageResponseDto;
import com.chat.yourway.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.util.UUID;

@Controller
@Slf4j
@RequiredArgsConstructor
public class ChatController {

private final SimpMessagingTemplate messagingTemplate;

public ChatController(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
private final MessageService messageService;

@MessageMapping("/app/topic/public/{topicId}")
public void sendMessage(@DestinationVariable String topicId, MessageResponseDto message) {
public void sendMessage(@DestinationVariable UUID topicId, MessageRequestDto message) {

log.info("Received message for topic ID: {}", topicId);

MessageResponseDto savedMessage = messageService.sendToTopic(topicId, message);
log.info("Message was saved in DB");

messagingTemplate.convertAndSend("/topic/public/" + topicId, message);
log.info("Message sent to topic ID: {}", topicId) ;

}
}

0 comments on commit 7000fbd

Please sign in to comment.