我想要實現我謨內的WebSocket做一些如通知SockJS了跺腳使用angular2春季啓動
這是我謨春季啓動
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").setAllowedOrigins("*").withSockJS();
}
,並在我的配置web套接字我控制器我有這樣的方法:
@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>
並在我的組件angular2我使用這個
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()
我認爲subscribe()參數應該是簡單的'/ topic/templatesWS' –
如何發送數據到服務器或獲取和事件後的對象? –
我解決這個錯誤,我有一些配置的WebSocket安全即阻止連接 但行代碼stompClient.subscribe沒有從控制器調用該方法 TemplatesWebSocket()我怎麼能稱之爲 –