Spring + StompWebsocket
Maven dependences
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3-1</version>
</dependency>
<script src="/webjars/sockjs-client/1.1.2/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/2.3.3-1/stomp.min.js"></script>
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/events”); // sending address to client
config.setApplicationDestinationPrefixes("/ws-app”); //listen client message
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket").withSockJS();
}
}
setConnected(connected) {
this.connected = connected;
console.log('connected: ', connected);
},
connect() {
const socket = new SockJS('/websocket');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, frame => {
this.setConnected(true);
this.stompClient.subscribe('/events/gate', res => {
const data = JSON.parse(res.body);
...
});
});
},
disconnect() {
if (this.stompClient !== null) {
this.stompClient.disconnect();
}
this.setConnected(false);
}
@Autowired
private SimpMessageSendingOperations messagingTemplate;
public void broadcastEvent(String path, Object message) {
messagingTemplate.convertAndSend("/events/" + path, message);
}
댓글
댓글 쓰기