// websocket
implementation 'org.springframework.boot:spring-boot-starter-websocket'
클라이언트 메세지 송신
→ console.log 출력
→ 클라이언트로 메세지 전송
@Slf4j(topic = "WebSocketChatHandler")
@Component
public class WebSocketChatHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload();
log.info("채팅 log : payload {}", payload);
TextMessage textMessage = new TextMessage("입장하셨습니다.");
session.sendMessage(textMessage);
}
}
WebSocket 활성화
- @EnableWebSocket 선언
- endpoint 설정(”/ws/chat”)
- CORS : setAllowedOrigins(”*”)
→ 도메인이 다른 서버에서도 접속 가능
@Slf4j(topic = "WebSocketChatHandler")
@Component
public class WebSocketChatHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload();
log.info("payload {}", payload);
TextMessage textMessage = new TextMessage("입장하셨습니다.");
session.sendMessage(textMessage);
}
}
(참고 : https://www.daddyprogrammer.org/post/4077/spring-websocket-chatting/)
(참고 : https://terianp.tistory.com/146)
(참고 : https://gnaseel.tistory.com/11)