我想用Vue的Spring websockets(STOMP),但不知道如何做,或者如果它甚至可能。我的websockets使用普通的JS,但是當我嘗試使用Vue時,我卡住了。這裏是我的VUE代碼:與vue.js的春天跺腳websockets
var app = new Vue({
el: '#app',
data: {
stompClient: null,
gold: 0
},
methods: {
sendEvent: function() {
this.stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
}
},
created: function() {
this.stompClient = Stomp.over(new SockJS('/gs-guide-websocket'));
this.stompClient.connect()
this.stompClient.subscribe('/topic/greetings', function (greeting) {
console.log(JSON.parse(greeting.body).content);
});
},
})
我的連接和發送功能正在工作,我可以看到在後端的消息,但問題是訂閱功能。它需要一個回調函數,但這永遠不會觸發。我也嘗試在vue中創建一個方法,並調用該方法
this.stompClient.subscribe('/topic/greetings', vueFunc())
但這也不起作用。我在https://github.com/FlySkyBear/vue-stomp找到了一些圖書館,但我無法弄清楚如何使用它,它看起來非常混亂。我寧願使用普通的JS。
任何人都有解決方案嗎?由於