다음학기에 스프링부트 + 플러터 + AI로 졸업 프로젝트를 진행합니다. 저는 백엔드로 스프링부트를 맡았습니다.
채팅 기능이 필요한데 한 번도 해본 적이 없어서 미리 공부해 둘 겸 찾아보았습니다.
아는 게 하나도 없는 상태라 우선 실행되는 걸 확인하고 더 찾아보면서 순차적으로 발전시켜 나가 보고자 합니다.
채팅은 보통 웹소켓을 사용한다고 합니다.
프로젝트 생성
start.spring.io 에서
Spring Web, WebSocket, Lombok 의존관계만 추가해서 프로젝트를 새로 생성해 주었습니다.
config/WebSocketConfig.java
package com.example.chat.config;
import com.example.chat.handler.ChatHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@RequiredArgsConstructor
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
private final ChatHandler chatHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(chatHandler, "ws/chat").setAllowedOrigins("*");
}
}
registerWebSocketHandlers 메서드에서
"ws/chat" 경로로 들어오는 웹소켓 통신을 chatHandler를 통해 처리하도록 설정해 줍니다.
chatHandler는 아래 작성된 ChatHandler클래스가 자동주입된 변수입니다.
모든 CORS 요청을 허용해 주기 위해 setAllowedOrigins("*")를 추가합니다.
handler/ChatHandler.java
package com.example.chat.handler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
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;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class ChatHandler extends TextWebSocketHandler {
private final static List<WebSocketSession> list = new ArrayList<>();
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for(WebSocketSession sess: list) {
sess.sendMessage(message);
}
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
list.add(session);
log.info(session + " 클라이언트 접속");
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
log.info(session + " 클라이언트 접속 해제");
list.remove(session);
}
}
Text, Binary 메시지를 다룰 수 있는 웹소켓 핸들러가 있는데, 텍스트를 이용할 거라 TextWebSocketHandler를 상속받아 만들었습니다.
WebSocketSession은 특별한 설정을 하지 않으면 스프링 웹소켓의 StandardWebSocketSession 구현체가 들어옵니다
- handleTextMessage
메시지가 왔을 때 실행되는 메서드입니다.
for문을 통해 연결되어 있는 모든 세션에 메시지를 다시 보내줍니다.
- afterConnectionEstablished, afterConnectionClosed
각각 연결이 만들어지고, 해제될 때 실행되는 메서드입니다. 세션이 모여있는 리스트에 세션을 추가해 주고, 제거합니다.
확인을 위한 로그도 찍어줍니다.
테스트
크롬 확장 프로그램인 Web Socket Client를 사용하여 테스트를 해보았습니다
창 두 개를 켜서 각각 ws://localhost:8080/ws/chat 으로 연결한 결과를 보여줍니다.
하나의 창에선 hello 1, 다른 창에서 hello 2 보내고 둘 다 응답을 잘 받은 모습을 확인할 수 있었습니다.
처음 연결했을 때 연결된 찍힌 로그와, 창을 닫고 클라이언트 접속 해제된 모습도 정상적으로 확인할 수 있었습니다.
다른 분들이 잘 써주신 글들이 너무 많아서 도움이 많이 되었습니다.
여러 채팅방 만들기, 보안, DB 저장은 어떤 방식으로 하는지 등을 더 공부해 봐야겠습니다.
참고
- https://dev-gorany.tistory.com/212
- https://velog.io/@sunkyuj/Spring-%EC%9B%B9%EC%86%8C%EC%BC%93%EC%9C%BC%EB%A1%9C-%EC%8B%A4%EC%8B%9C%EA%B0%84-%EC%B1%84%ED%8C%85-%EA%B5%AC%ED%98%84
'Java' 카테고리의 다른 글
SpringBoot WebSocket 채팅기능 만들기 (2) - STOMP (0) | 2024.02.12 |
---|---|
[Java] 람다 표현식 (0) | 2023.09.06 |
[Java] 동작 파라미터화 전달하기 (0) | 2023.08.31 |
[Java] Collections Framework 정리 (1) (0) | 2023.08.29 |