我希望在我的项目中实现一个websocket,用于处理通知。
这是我在Spring Boot项目中的websocket配置。
在我的客户端页面index.html中,我添加了以下内容:
我该如何调用TemplatesWebSocket()方法?
这是我在Spring Boot项目中的websocket配置。
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").setAllowedOrigins("*").withSockJS();
}
在我的控制器中,我有这个方法:
@MessageMapping("/hello")
@SendTo("/topic/templatesWS")
public void TemplatesWebSocket() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("calling juste");
}
在我的客户端页面index.html中,我添加了以下内容:
<script src="//cdn.jsdelivr.net/sockjs/1/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>
在我的Angular 2组件中,我正在使用以下代码:
declare let SockJS;
declare let Stomp;
var socket = new SockJS('http://localhost:8080/hello');
socket.ono
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('http://localhost:8080/template/topic/templatesWS', function(greeting) {
console.log("from from", greeting);
});
}, function (err) {
console.log('err', err);
});
我该如何调用TemplatesWebSocket()方法?