양방향
네트워크 프로토콜 기반으로 동작Binary 데이터
를 포함 할 수 있다.pub/sub
구조로 동작@MessagingMapping
어노테이션을 사용해 메시지 발행 시 엔드포인트를 별로도 분리해서 관리할 수 있다.COMMAND
header1:value1
header2:value2
Body^@
SUBSCRIBE
id:sub-1
destination:/sub/cache/karim
^@
SEND
destination:/pub/cache/karim
content-type:application/json
content-length:44
{"channelId":"karim"}^@
MESSAGE
message-id:nxahklf6-1
subscription:sub-1
destination:/sub/cache/karim
{"channelId":"karim"}^@
IntelliJ : 2022.1.3.Ultimate
spring boot : 2.6.1
implementation 'org.springframework.boot:spring-boot-starter-websocket'
@Configuration
public class ClientWebSocketStompConfig {
@Bean
public WebSocketStompClient WebSocketStompClient(WebSocketStompClient webSocketClient,
StompSessionHandler stompSessionHandler) {
// client to server message converter
webSocketClient.setMessageConverter(new MappingJackson2MessageConverter());
StompHeaders stompHeaders = new StompHeaders();
stompHeaders.add("host", "karim");
Object[] urlVariables = {};
String url = "wss://localhost:8443/ws";
webSocketClient.connect(url, null, stompHeaders, stompSessionHandler, urlVariables);
return webSocketClient;
}
@Bean
public WebSocketStompClient webSocketClient() {
WebSocketClient webSocketClient = new StandardWebSocketClient();
return new WebSocketStompClient(webSocketClient);
}
@Bean
public StompSessionHandler stompSessionHandler() {
return new ClientWebSocketStompSessionHandler();
}
}
public class ClientWebSocketStompSessionHandler extends StompSessionHandlerAdapter {
@Override
public void handleFrame(StompHeaders headers, Object payload) {
// 구독한 채널의 메세지 받기
System.out.println("SpringStompSessionHandler.handleFrame");
System.out.println("headers = " + headers);
System.out.println("payload = " + new String((byte[]) payload));
}
@Override
public Type getPayloadType(StompHeaders headers) {
return Object.class;
}
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
// 구독
session.subscribe("/sub/cache/karim", this);
Map<String, Object> params = new HashMap<>();
params.put("channelId", "karim");
// 메세지 보냄
session.send("/sub/cache/karim", params);
System.out.println("params = " + params);
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
System.out.println("SpringStompSessionHandler.handleException");
System.out.println("exception = " + exception);
}
@Override
public void handleTransportError(StompSession session, Throwable exception) {
// 이부분 서버 꺼져서 처음에 못붙거나, 붙었다가 서버 꺼지면 나옴 이때 재 커넥션 와일 돌리면 될꺼같음
System.out.println("SpringStompSessionHandler.handleTransportError");
}
}
📌 여담
📚 참고