Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- gradle
- error
- Docker
- MongoDB
- vue.js
- Linux
- docker network
- grpc
- JavaScript
- Spring Boot
- jpa
- 리액트
- grafana
- jenkins github
- spring
- IntelliJ
- Jenkins
- Jenkins Pipeline
- jenkins install
- jenkins jdk
- jenkins maven
- nginx
- subnetmask
- jenkins github 연동
- 리눅스
- CI/CD
- REACT
- java
- MySQL
- jenkins 설치
Archives
- Today
- Total
뭐든 즐기면서 ;)
Spring Boot WebSocket server 구현 기본(HandshakeInterceptor 사용) 본문
BACK/Spring Boot & JPA
Spring Boot WebSocket server 구현 기본(HandshakeInterceptor 사용)
Tada.*+ 2023. 7. 19. 23:50728x90
WebSocket 개념에 대한 것은 무수히도 많은, 좋은 글들이 많기 때문에 생략하고, 실 구현에 필요한 코드만 쏘심플하게 작성했다.
1. dependencies 추가
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-websocket'
...
}
2. 소켓 구현
package ai.maum.bsbf.conf.websocket;
import ai.maum.bsbf.util.LogUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.*;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler(), "/ws") // websocket uri
.setAllowedOrigins("*"); // CORS 모든 url 허용 (실 개발 시에는 모든 url을 허용해서는 안 됨.)
}
@Bean
public WebSocketHandler webSocketHandler() {
return new WebSocketHandler() {
private final Map<String, WebSocketSession> CLIENTS = new ConcurrentHashMap<>(); // 웹 소켓 연결 session 모음
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 웹 소켓 연결 시
CLIENTS.put(session.getId(), session);
LogUtil.info("웹 소켓 연결 : " + session.getId());
session.sendMessage(new TextMessage("접속됨."));
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { // 데이터 통신 시
session.sendMessage(message);
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // 웹소켓 통신 오류 시
LogUtil.debug("웹 소켓 통신 오류 : " + session.getId());
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // 웹소켓 연결 종료 시
CLIENTS.remove(session.getId());
LogUtil.info("웹 소켓 종료 : " + session.getId());
}
@Override
public boolean supportsPartialMessages() {
return false;
}
};
}
}
3. 연결 테스트를 해본다.
728x90
'BACK > Spring Boot & JPA' 카테고리의 다른 글
Spring boot Error : A problem occurred configuring root project (0) | 2023.05.09 |
---|---|
Spring Security 기본 설정 (0) | 2023.04.28 |
Spring Boot + Gradle Multi Module (0) | 2022.11.18 |
JPA persistence.xml / JPA DB설정 (0) | 2021.11.27 |
Spring Boot(Gradle) + MySQL + JPA(Hibernate) [3] - Spring Data JPA 설정 2 (0) | 2021.10.27 |
Comments