我正在尝试在Vue中使用Spring Websockets (STOMP),但无法弄清楚如何实现,或者它是否可能。 我的Websockets可以通过纯JS工作,但是当我尝试使用Vue时就会卡住。 这是我的Vue代码:
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);
});
},
我的connect和send函数工作正常,我可以在后端看到消息,但问题在于subscribe函数。它需要一个回调函数,但这个函数从未触发过。我还尝试过在Vue中创建一个方法并调用它。
this.stompClient.subscribe('/topic/greetings', vueFunc())
但这也行不通。我在https://github.com/FlySkyBear/vue-stomp找到了一些库,但我无法弄清如何使用它,而且看起来很混乱。我宁愿使用普通JS。
有人有解决方案吗?谢谢
.setAllowedOrigins("*")
。谢谢! - Maurice Müller